int update = resolver.update(uri, values, "id=?", new String[] { 6 + "" });
System.out.println("更新了" + update + "行");
}
/** 测试查询 */
public void testQueryOne() {
ContentResolver resolver = this.getContext().getContentResolver();
Uri uri = Uri.parse("content://com.itheima.sqlite.provider/person");
Cursor c = resolver.query(uri, new String[] { "name", "balance" }, "id=?", new String[] { 101 + "" }, null);
if (c.moveToNext()) {
System.out.print(c.getString(0));
System.out.println(" " + c.getInt(1));
}
c.close();
}
/**测试查询全部 */
public void testQueryAll() {
ContentResolver resolver = this.getContext().getContentResolver();
Uri uri = Uri.parse("content://com.itheima.sqlite.provider/person");
Cursor c = resolver.query(uri, new String[] { "id", "name", "balance" }, null, null, "name desc");
while (c.moveToNext()) {
System.out.println(c.getInt(0) + ", " + c.getString(1) + ", " + c.getInt(2));
}
c.close();
}
/** 测试查询一条 */
public void testQueryOneWithUriId() {
ContentResolver resolver = this.getContext().getContentResolver();
Uri uri = Uri.parse("content://com.itheima.sqlite.provider/3"); // 查询 ID 为 3 的记录
Cursor c = resolver.query(uri, new String[] { "id", "name", "balance" }, null, null, null);
if (c.moveToNext()) {
System.out.println(c.getInt(0) + ", " + c.getString(1) + ", " + c.getInt(2));
}
c.close();
}
/** 测试获取内容提供者的返回类型 */
public void testGetType() {
ContentResolver resolver = this.getContext().getContentResolver();
System.out.println(resolver.getType(Uri.parse("content://com.itheima.sqlite.provider/2")));
System.out.println(resolver.getType(Uri.parse("content://com.itheima.sqlite.provider/person")));
}
}
4、监听内容提供者的数据变化
在内容提供者中可以通知其他程序数据发生变化
通过 Context 的 getContentResolver()方法获取 ContentResolver
调用其 notifyChange()方法发送数据修改通知,发送到系统的公共内存(消息信箱中)