Content Provider Resolver

xiaoxiao2021-02-28  114

Content Provider Resolver     最近学习Provider Resolver 的过程中遇到了些许问题,现在一一分享一下,希望和我一样刚开始学习android 的菜鸟看了我的这篇博客有所收获。 为什么要有ContentProvider:     功能需求:一个应用需要访问另一个应用的数据库表数据     实际情况:一个应用的数据文件是应用私有的,其他应用不能直接访问。 当前应用使用ContentProvider将数据库表操作暴露给其他应用访问,其他应用需要使用ContentResolver来调用ContentProvider的方法。它们之间的调用是通过Uri来进行交流的 <provider android:authorities="com.example111.sxy.l_contentprovider.personprovider" android:name="com.example111.sxy.l_contentprovider.PersonProvider" android:exported="true"/>

这是写Provider需要在AndroidManifest文件中添加这些。特别是最后一项。

另外写Provider不需要界面。

用来存放所有合法的Uri的容器

private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); static { matcher.addURI("com.example111.sxy.l_contentprovider.personprovider","person",1); matcher.addURI("com.example111.sxy.l_contentprovider.personprovider","person/#",2);}

存放一些合法的Uri  有id 没有id

query()   insert()   delete() update()  方法步骤如下:

1 得到连接对象

2 匹配uri 返回code

3 如果合法  进行查询 (根据id,不根据id)不合法就抛异常。

@Overridepublic Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) { //得到连接对象 SQLiteDatabase database = dbHelper.getReadableDatabase(); //1 匹配uri,返回code int code = matcher.match(uri); //2 如果合法 进行查询(根据id,不根据id) 不合法就抛异常 if(code==1){ Cursor cursor = database.query("person",strings,s,strings1,null,null,null); return cursor; }else if (code==2){ //既然是根据id查就得先得到id Long id = parseId(uri); Cursor cursor = database.query("person",strings,"_id=?",new String[]{id+""},null,null,null); return cursor; }else { throw new RuntimeException("异常"); }}

插入只能是不根据id插入

@Override//只能是没有id插入public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) { SQLiteDatabase database = dbHelper.getReadableDatabase(); int code = matcher.match(uri); if(code==1){ Long id = database.insert("person",null,contentValues); uri = ContentUris.withAppendedId(uri,id); database.close(); return uri; }else { database.close(); throw new RuntimeException("插入异常"); }}

删除可以根据内容删除也可以根据id删除

@Overridepublic int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) { SQLiteDatabase database = dbHelper.getReadableDatabase(); int code = matcher.match(uri); int deleteCount = -1; if(code == 1){ deleteCount = database.delete("person",s,strings); database.close(); }else if(code == 2){ Long id = ContentUris.parseId(uri); deleteCount = database.delete("person","_id="+id,null); database.close(); }else { database.close(); throw new RuntimeException("删除错误"); } return deleteCount;}

更新可以根据内容也可以根据id更新

public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) { SQLiteDatabase database = dbHelper.getReadableDatabase(); int code = matcher.match(uri); int updateCount = -1; if(code == 1){ updateCount = database.update("person",contentValues,s,strings); database.close(); }else if (code == 2){ Long id = ContentUris.parseId(uri); updateCount = database.update("person",contentValues,"_id="+id,null); database.close(); }else { database.close(); throw new RuntimeException("删除错误"); } return updateCount;}

ContentResolver应用需要界面 

query() insert() update() delete()

1 得到ContentResolver 对象

2 调用其query() 得到cursor

3 取出cursor中的数据, 并显示

public void query(View v){ //1 得到ContentResolver对象 ContentResolver resolver = getContentResolver(); //2 调用其query 得到cursor Uri uri = Uri.parse("content://com.example111.sxy.l_contentprovider.personprovider/person"); Cursor cursor = resolver.query(uri,null,null,null,null); //3 取出cursor中的数据,并显示 while (cursor.moveToNext()){ int id = cursor.getInt(0); String name = cursor.getString(1); Toast.makeText(this,id+":"+name,Toast.LENGTH_LONG).show(); }} public void insert(View v){ ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://com.example111.sxy.l_contentprovider.personprovider/person"); ContentValues contentValues = new ContentValues(); contentValues.put("name","haha"); uri = resolver.insert(uri,contentValues); Toast.makeText(this,uri.toString(),Toast.LENGTH_LONG).show();} public void update(View v){ ContentResolver contentResolver = getContentResolver(); Uri uri = Uri.parse("content://com.example111.sxy.l_contentprovider.personprovider/person/2"); ContentValues contentValues = new ContentValues(); contentValues.put("name","ll"); int count = contentResolver.update(uri,contentValues,null,null); Toast.makeText(this,"count"+count,Toast.LENGTH_LONG).show();} public void delete(View v){ ContentResolver contentResolver = getContentResolver(); Uri uri = Uri.parse("content://com.example111.sxy.l_contentprovider.personprovider/person/2"); int delectCount = contentResolver.delete(uri,null,null); Toast.makeText(this,"delectCount"+delectCount,Toast.LENGTH_LONG).show();}

以上。

转载请注明原文地址: https://www.6miu.com/read-50228.html

最新回复(0)