Android开发——数据存储之ContentProvider(2)

----------------接上文---------------------

2、创建自定义ContentProvider
创建内容供应器,需要继承ContentProvider
 
示例代码三:
   1:  
   2: /**
   3:  * 内容提供者
   4:  * @author 张汉国
   5:  */
   6: public class TodoListProvider extends ContentProvider {
   7:  
   8:     private static final String TAG = TodoListProvider.class.getSimpleName();
   9:  
  10:     //数据库名
  11:     private static final String DATABASE_NAME = "todolist.db";
  12:     //版本
  13:     private static final int DATABASE_VERSION = 1;
  14:     //表名
  15:     private static final String TABLE_NAME = "todos";
  16:     //属性表
  17:     private static HashMap<String, String> sTodosProjectionMap;
  18:  
  19:     private static final int TODOS = 1;
  20:     private static final int TODO_ID = 2;
  21:  
  22:     //uri匹配器
  23:     private static final UriMatcher uriMatcher;
  24:  
  25:     private DataBaseHelper dataBaseHelper;
  26:  
  27:     static {
  28:         //数据初始化
  29:         uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  30:         uriMatcher.addURI(TodoList.AUTHORITY, "todos", TODOS);
  31:         uriMatcher.addURI(TodoList.AUTHORITY, "todos/#", TODO_ID);
  32:  
  33:         sTodosProjectionMap = new HashMap<String, String>();
  34:         sTodosProjectionMap.put(Todo._ID, Todo._ID);
  35:         sTodosProjectionMap.put(Todo.TITLE, Todo.TITLE);
  36:         sTodosProjectionMap.put(Todo.CONTENT, Todo.CONTENT);
  37:         sTodosProjectionMap.put(Todo.CREATED_DATE, Todo.CREATED_DATE);
  38:         sTodosProjectionMap.put(Todo.MODIFIED_DATE, Todo.MODIFIED_DATE);
  39:     }
  40:  
  41:     private static class DataBaseHelper extends SQLiteOpenHelper {
  42:  
  43:         public DataBaseHelper(Context context) {
  44:             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  45:         }
  46:  
  47:         //创建表
  48:         @Override
  49:         public void onCreate(SQLiteDatabase db) {
  50:             Log.d(TAG, "create table :" + TABLE_NAME);
  51:             db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + Todo._ID + " INTEGER PRIMARY KEY," + Todo.TITLE
  52:                     + " TEXT," + Todo.CONTENT + " TEXT," + Todo.CREATED_DATE + " INTEGER," + Todo.MODIFIED_DATE
  53:                     + " INTEGER)");
  54:             ContentValues values = new ContentValues();
  55:             values.put(Todo.TITLE, "test");
  56:             values.put(Todo.CONTENT, "dddddddd");
  57:             db.insert(TABLE_NAME, "title", values);
  58:         }
  59:  
  60:         //更新数据库表
  61:         @Override
  62:         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  63:             Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion
  64:                     + ", which will destroy all old data");
  65:             db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  66:             onCreate(db);
  67:         }
  68:  
  69:     }
  70:  
  71:     //删除数据操作
  72:     @Override
  73:     public int delete(Uri uri, String selection, String[] selectionArgs) {
  74:         SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
  75:         int count = 0;
  76:         //判断uri类型
  77:         switch (uriMatcher.match(uri)) {
  78:         case TODOS:
  79:             //如果以"/todo"结尾,则删除整个表数据
  80:             count = db.delete(TABLE_NAME, selection, selectionArgs);
  81:             break;
  82:         case TODO_ID:
  83:             //如果以“/todo/1”结尾,则删除ID=1的数据
  84:             String todoId = uri.getPathSegments().get(1);
  85:             count = db.delete(TABLE_NAME, Todo._ID + "=" + todoId
  86:                     + ((TextUtils.isEmpty(selection)) ? "" : " AND " + selection), selectionArgs);
  87:             break;
  88:         default:
  89:             throw new IllegalArgumentException("Unknown URI :" + uri);
  90:         }
  91:         getContext().getContentResolver().notifyChange(uri, null);
  92:         return count;
  93:     }
  94:     ......
  95:     ......
  96:     //其他方法省略

至此我们就创建了一个ContentProvider了

3、Provider的配置,在AndroidMainfest.xml配置

示例代码三:

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   3:     package="com.halzhang.android.provider" android:versionCode="1"
   4:     android:versionName="1.0">
   5:     <application android:icon="@drawable/icon" android:label="@string/app_name">
   6:         <!-- 
   7:     name属性为继承ContentProvider的类
   8:     authorities属性为属性对象路径
   9:      -->
  10:         <provider android:name=".TodoListProvider"
  11:             android:authorities="com.halzhang.android.provider.TodoList"></provider>
  12:         <activity android:name=".ContentProviderDemo" android:label="@string/app_name">

 

OK,至此我们就创建了一个ContentProvider了。

你可能感兴趣的:(ContentProvider)