四大组件---ContentResolver
内容提供器的用法一般有两种:
(1) 使用现有的内容提供器来读取和操作相应程序的数据
(2) 创建自己的内容提供器给我们程序的数据提供外部接口。
一、 ContentResolver的基本用法
ContentResolver是通过URI来查询ContentProvider中提供的数据。
内容URI标准写法如下:
content://com.example.app.provider/table1
内容URI字符串能清楚的表达出是哪个程序的哪个表。ContentResolver中的增删改查方法接收内容URI。需要把URI字符串解析成URI对象。调用Uri.parse()方法即可解析。
Uri uri = Uri.parse("content://com.example.app.provider/table1")
用URI对象查询表table1表中数据
Cursor cursor = getContentResolver().query(
uri,
projection,
selection,
selectionArgs,
sortOrder);
查询完成后返回的仍然是Cursor对象,将数据从crsor对象中逐个读取
if(cursor != null){
while(cursor.moveToNext()){
String column1 = cursor.getString(cursor.getColunmIndex("colunm1"));
int column2 = cursor.getInt(cursor.getColumnIndex("column2");
}
cursor.close();
}
添加
ContentValues values = new ContentValues();
values.put("column1", "text");
valuse.put("column2"