ContentResolver.query详解

1.查询手机的联系人
    public void getContacts() {
        ContentResolver contentResolver = this.getContentResolver();
        Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                Log.d(TAG,cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID)));
                Log.d(TAG,cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)));
            } while (cursor.moveToNext());
        }
        cursor.close();
    }

记得开权限

 <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />

log

05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 1
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 阿大
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 2
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 阿牛
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 3
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 阿虎
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 4
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 海哥
cusor的正确遍历方式

查询得到的cursor是指向第一条记录之前的,因此查询得到cursor后第一次调用moveToFirst或moveToNext都可以将cursor移动到第一条记录上。但是为了规避一些可能存在的情况,下面这种写法比较保险

if (cursor != null && cursor.moveToFirst())
{
      do{

      }while(cursor.moveToNext());
}
cursor.close();

这篇文章主要讲解ContentResolver的query方法。

ContentResolver直译为内容解析器,什么东东?Android中程序间数据的共享是通过Provider/Resolver进行的。提供数据(内容)的就叫Provider,Resovler提供接口对这个内容进行解读。

根据Android文档,

public final Cursor query (Uri uri, String[] projection,String selection,String[] selectionArgs,String sortOrder)

第一个参数,uri

uri是什么呢?好吧,上面我们提到了Android提供内容的叫Provider,那么在Android中怎么区分各个Provider?有提供联系人的,有提供图片的等等。所以就需要有一个唯一的标识来标识这个Provider,Uri就是这个标识,android.provider.ContactsContract.Contacts.CONTENT_URI就是提供联系人的内容提供者,可惜这个内容提供者提供的数据很少。

第二个参数,projection,

这个参数告诉Provider要返回的内容(列Column),比如Contacts Provider提供了联系人的ID和联系人的NAME等内容,如果我们只需要NAME,那么我们就应该使用:

Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,  
    new String[]{android.provider.ContactsContract.Contacts.DISPLAY_NAME}, null, null, null);  

当然,下面打印的你就只能显示NAME了,因为你返回的结果不包含ID。用null表示返回Provider的所有内容(列Column)。
Log

05-08 16:00:55.587 13524-13524/com.bbk.contentprovidertest D/notecontent: 阿大
05-08 16:00:55.588 13524-13524/com.bbk.contentprovidertest D/notecontent: 阿牛
05-08 16:00:55.588 13524-13524/com.bbk.contentprovidertest D/notecontent: 阿虎
05-08 16:00:55.588 13524-13524/com.bbk.contentprovidertest D/notecontent: 海哥

第三个参数,selection,

设置条件,相当于SQL语句中的where。null表示不进行筛选。如果我们只想返回名称为“海哥”的数据,第三个参数应该设置为:

 Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,
                new String[]{ContactsContract.Contacts.DISPLAY_NAME}, ContactsContract.Contacts.DISPLAY_NAME + "= '海哥'", null, null);//注意‘海哥’ 

获取到的结果,Log

05-08 16:05:16.264 20723-20723/com.bbk.contentprovidertest D/notecontent: 海哥

例如查询手机中以.txt结尾的文件

Cursor cursor = getContentResolver().query(
                Uri.parse("content://media/external/file"),
                projection,
                MediaStore.Files.FileColumns.DATA + " like ?" ,
                new String[]{"%.txt"},
                null);

SQL模糊查询语句

先看下面这个函数,查询手机中以.txt结尾的文件以及文档大小要超过500KB的文档。

  private void queryBooks() {
        String[] projection = new String[]{MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE};
        //查询以.txt结尾的文件以及文件大小超过1024*500(500KB)
        //注意 AND  俩边空格 
        Cursor cursor = getContentResolver().query(
                Uri.parse("content://media/external/file"),
                projection,
                MediaStore.Files.FileColumns.DATA + " like ?" + " AND " + MediaStore.Files.FileColumns.SIZE + " >= ?",
                new String[]{"%.txt", "512000"},
                null);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                int ctitle = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
                int csize = cursor.getColumnIndex(MediaStore.Files.FileColumns.SIZE);
                Recommend.RecommendBooks books = new Recommend.RecommendBooks();
                long size = cursor.getLong(csize);
                String title = cursor.getString(ctitle);
                int dot = title.lastIndexOf("/");
                String name = title.substring(dot + 1);
                if (name.lastIndexOf(".") > 0) {
                    name = name.substring(0, name.lastIndexOf("."));
                }
                books.title = name;
                books.lastChapter = FileUtils.formatFileSizeToString(size);
                books.isFromSD = true;
                mList.add(books);
            } while (cursor.moveToNext());
          
        }
        cursor.close();
        mAdapter.setListItems(mList);
        mAdapter.notifyDataSetChanged();
        mListView.setAdapter(mAdapter);

    }

上面这段代码直接在查询时,就明确了要求,查询效率也大幅度提升,比在查询后根据size再次判断快了200多毫秒。

SQL模糊查询,使用like比较字,加上SQL里的通配符:

like使用:

下面一些实例演示了 带有 ‘%’ 和 ‘_’ 运算符的 LIKE 子句不同的地方:

语句描述
WHERE SALARY LIKE ‘200%’查找以 200 开头的任意值
WHERE SALARY LIKE ‘%200%’查找任意位置包含 200 的任意值
WHERE SALARY LIKE ‘_00%’查找第二位和第三位为 00 的任意值
WHERE SALARY LIKE ‘2_%_%’查找以 2 开头,且长度至少为3个字符的任意值
WHERE SALARY LIKE ‘%2’查找以 2 结尾的任意值
WHERE SALARY LIKE ‘_2%3’查找第二位为 2,且以 3 结尾的任意值
WHERE SALARY LIKE ‘2___3’查找长度为 5位数,且以2开头以3结尾的任意值

第四个参数,selectionArgs,

这个参数是要配合第三个参数使用的,如果你在第三个参数里面有?,那么你在selectionArgs写的数据就会替换掉?,

  Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,
                new String[]{ContactsContract.Contacts.DISPLAY_NAME}, ContactsContract.Contacts.DISPLAY_NAME + "= ?", new String[]{"海哥"}, null);

效果和上面一句的效果一样。

第五个参数,sortOrder,

按照什么进行排序,相当于SQL语句中的Order by。如果想要结果按照ID的降序排列:

Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,  
                null, null,null, android.provider.ContactsContract.Contacts._ID + " DESC");

Log

05-08 16:18:11.061 4921-4921/com.bbk.contentprovidertest D/notecontent: 海哥
05-08 16:18:11.061 4921-4921/com.bbk.contentprovidertest D/notecontent: 阿虎
05-08 16:18:11.061 4921-4921/com.bbk.contentprovidertest D/notecontent: 阿牛
05-08 16:18:11.061 4921-4921/com.bbk.contentprovidertest D/notecontent: 阿大

升序,默认排序是升序,也可+" ASC":


Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,  
                null, null,null, android.provider.ContactsContract.Contacts._ID + " ASC"); 

Log

05-08 16:18:53.787 6316-6316/com.bbk.contentprovidertest D/notecontent: 阿大
05-08 16:18:53.787 6316-6316/com.bbk.contentprovidertest D/notecontent: 阿牛
05-08 16:18:53.787 6316-6316/com.bbk.contentprovidertest D/notecontent: 阿虎
05-08 16:18:53.787 6316-6316/com.bbk.contentprovidertest D/notecontent: 海哥

  • 11
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值