1,建立一个属于自己的content provider你必须要做
- 设置一个存储数据的地方,可以使用android 提供的文件存储方法,或者使用sqlite databases,然而,你可以存储任何你想要的数据,android提供的sqliteOpenHelper可以帮助你创建属于和管理你自己的数据库,
- 实现ContentProvider 提供用于访问数据的类
- 在AndroidManifest.xml,声明你的 content provider
2,编写一个数据工具类
- publicclassContentProviderData{
- publicstaticfinalStringAUTHORIY="kg.tom.FirstProvider";
- publicclassContentProviderData{
- //定义我们provider使用的认证uri
- publicstaticfinalStringAUTHORIY="kg.tom.FirstProvider";
- //定义我们数据库的名字
- publicstaticfinalStringDATABASE_NAME="kg_tom_pro_test";
- //定义数据库的版本号
- publicstaticfinalintDATABASE_VERSION=1;
- //定义创建表的名字
- publicstaticfinalStringUSERS_TABLE_NAME="users";
- //实现BaseColums,自带两列,_id,_count
- publicstaticfinalclassUserTableMetaDataimplementsBaseColumns{
- //定义创建表的名字
- publicstaticfinalStringTABLE_NAME="users";
- //定义provider的uri
- publicstaticfinalUriCONTENT_URI=Uri.parse("content://"+AUTHORIY+"/users");
- //定义一条数据的类型
- publicstaticfinalStringCONTENT_TYPE="vnd.android.cursor.dir/vnd.firstprovider.user";
- //定义一组数据的类型
- publicstaticfinalStringCONTENT_TYPE_ITEM="vnd.android.cursor.item/vnd.fiRSStprovider.user";
- //定义列名
- publicstaticfinalStringUSER_NAME="name";
- //定义排序规则
- publicstaticfinalStringDEFULT_SORT_ORDER="_iddesc";
- }}
2,继承content provider 类 继承这个类我们同时要实现以下几个方法: query() insert() update() delete() getType() onCreate() 还有必须添加一个UriMatcher对象 关于UriMatcher对象的使用文档有很好的解释
- //定义URI标识符,数值定义请参考文档
- publicstaticfinalintINCOMING_USER_COLLECTION=1;
- publicstaticfinalintINCOMING_USER_SINGLE=2;
- //定义UriMatcher对象
- publicstaticfinalUriMatcheruMatcher;
- //把标识符加入到URI中,固定写法
- static{
- uMatcher=newUriMatcher(UriMatcher.NO_MATCH);
- //第一个参数为
- contentproviderauthoriy
- uMatcher.addURI(AUTHORIY,"users",INCOMING_USER_COLLECTION);
- uMatcher.addURI(AUTHORIY,"users/#",INCOMING_USER_SINGLE);}
一个完整的content provider
- publicclassFirstProviderextendsContentProvider{
- //定义URI标识符
- publicstaticfinalintINCOMING_USER_COLLECTION=1;
- publicstaticfinalintINCOMING_USER_SINGLE=2;
- //定义UriMatcher对象
- publicstaticfinalUriMatcheruMatcher;
- //把标识符加入到URI中,固定写法
- static{
- uMatcher=newUriMatcher(UriMatcher.NO_MATCH);uMatcher.addURI(ContentProviderData.AUTHORIY,INCOMING_USER_COLLECTION);
- uMatcher.addURI(ContentProviderData.AUTHORIY,INCOMING_USER_SINGLE);}
- //这里是定义查询用的列名的别名
- publicstaticHashMap<String,String>userMap;
- privateDataBaseHelperdh;
- static{
- userMap=newHashMap<String,String>();
- userMap.put(UserTableMetaData._ID,"id");
- userMap.put(UserTableMetaData.USER_NAME,
- UserTableMetaData.USER_NAME);}
- @Override
- publicintdelete(Uriuri,Stringselection,String[]selectionArgs){
- //TODOAuto-generatedmethodstub
- return0;}
- //根据传入的URI
- @Override
- publicStringgetType(Uriuri){
- //TODOAuto-generatedmethodstub
- Log.d("sqlite","getType-->");
- switch(uMatcher.match(uri)){
- caseINCOMING_USER_COLLECTION:
- returnUserTableMetaData.CONTENT_TYPE;
- caseINCOMING_USER_SINGLE:
- returnUserTableMetaData.CONTENT_TYPE_ITEM;default:
- thrownewIllegalArgumentException("unKnownuri"+uri);}}
- @Override
- publicUriinsert(Uriuri,ContentValuesvalues){
- //TODOAuto-generatedmethodstub
- Log.d("insert","class"+getClass());
- Log.d("insert","content"+getContext());
- sqliteDatabasedb=dh.getWritableDatabase();
- longrowId=db.insert(UserTableMetaData.TABLE_NAME,values);
- if(rowId>0){
- UriinsertUri=ContentUris.withAppendedId
- (UserTableMetaData.CONTENT_URI,rowId);
- getContext().getContentResolver().notifyChange(insertUri,null);
- returninsertUri;}
- returnnull;}
- @Override
- publicbooleanonCreate(){
- //TODOAuto-generatedmethodstub
- Log.d("sqlite","createPro--->");
- dh=newDataBaseHelper(getContext(),ContentProviderData.DATABASE_NAME);
- //sqliteDatabasedb=dh.getReadableDatabase();returntrue;}
- @Override
- publicCursorquery(Uriuri,String[]projection,
- String[]selectionArgs,StringsortOrder){
- //TODOAuto-generatedmethodstub
- sqliteQueryBuilderqb=newsqliteQueryBuilder();
- switch(uMatcher.match(uri)){
- caseINCOMING_USER_COLLECTION:
- qb.setTables(UserTableMetaData.TABLE_NAME);
- qb.setProjectionMap(userMap);
- break;
- caseINCOMING_USER_SINGLE:
- qb.setTables(UserTableMetaData.TABLE_NAME);
- qb.setProjectionMap(userMap);
- qb.appendWhere(UserTableMetaData._ID+"="+
- uri.getPathSegments().get(1));
- break;}
- StringorderBy;
- if(TextUtils.isEmpty(sortOrder)){
- orderBy=UserTableMetaData.DEFULT_SORT_ORDER;}else{
- orderBy=sortOrder;}
- sqliteDatabasedb=dh.getWritableDatabase();
- Cursorc=qb.query(db,projection,selection,
- selectionArgs,sortOrder);
- c.setNotificationUri(getContext().getContentResolver(),uri);
- Log.d("sqlite","first--->>");returnc;}
- @Override
- publicintupdate(Uriuri,ContentValuesvalues,
- String[]selectionArgs){
- //TODOAuto-generatedmethodstub
- return0;}}
3,在activity中的方法中用上这几行代码就可以进行插入
- //定义插入用数据的对象,本质是一个
- mapContentValuesvalues=newContentValues();
- //键,值对values.put("id",1);
- //定义表名
- Stringname=ContentProviderData.USERS_TABLE_NAME;
- //键值对
- values.put(ContentProviderData.UserTableMetaData.USER_NAME,"tom");
- //进行contentprovider的插入操作
- Uriuri=getContentResolver().insert(
- ContentProviderData.UserTableMetaData.CONTENT_URI,values);
至于剩下的 query() delete() 自己参照文档补全吧 |