四大组件---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"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值