android_基础_ContentResolver.query方法的使用(从通讯录查询电话号码相关Api解析)

121 篇文章 1 订阅

转载自:http://blog.csdn.net/supluo/article/details/43954129

以下是文档上面的介绍:

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

Added in API level 1

Query the given URI, returning a [Cursor](http://www.android-doc.com/reference/android/database/Cursor.html) over the result set.

For best performance, the caller should follow these guidelines:

  • Provide an explicit projection, to prevent reading data from storage that aren’t going to be used.
  • Use question mark parameter markers such as ‘phone=?’ instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.
Parameters

在这里插入图片描述

Returns
  • A Cursor object, which is positioned before the first entry, or null

这个方法的使用主要是了解参数的含义,很多时候我们在使用时,后面的几个参数大多都传递的是null,其实好好了解下这些参数,能够更好的使用这些方法。上面的有几句英文提示和后面的参数使用是促使我记录这个方法使用的原因。

For best performance, the caller should follow these guidelines:

  • Provide an explicit projection, to prevent reading data from storage that aren’t going to be used.
  • Use question mark parameter markers such as ‘phone=?’ instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.

这几句话的意思是说,为了更好的使用这个方法,调用者需要了解下面的两条知道原则:

1、提供明确的Projection参数来避免读取不会使用的数据字段。什么意思呢,举个例子,比如我们数据库存储了Persion这么一张记录表,有name,age等字段,如果要使用这个方法获取name字段,就明确指出第二个参数的值,这样就不用再去把age等这些不用的字段读取出来(读取数据库是需要消耗资源的);

2、在selection参数中使用诸如标记参数的形式比如’phone=?’ 而不使用明确参数的形式,比如’phone=189xxx’,这样做使得这些值会被视作为相同的缓存目的(caching purposes不知怎么翻译好),也就是说使用参数标记的形式是需要selectionArgs这个参数来支持的,而使用这种形式会使得selectionArgs会被作为缓存目的之类的东西来使用,这样查询效率应该更快一些。大致就是这个意思,这里描绘不当请见谅。

好的应用就是要注意这些细节地方,细节决定成败啊!

由于我们没有怎么使用过后面这些参数,因此这里简单举个例子进行对比使用

假如我们要实现: select * from myTable where var=‘const’

1 、那么myTable 就是URI

2、* 就是projection

3、selection可以是 “var=?”

4、那么selectionArgs就是 new String{“const”} 也可以是“var = ‘const’”,那么selectionArgs就是null 但是上面的指导原则说明了我们应该采用前者而不应该使用后者

5、sortOrder 最后一个参数就是排列顺序的意思。

下面贴上我之前遇到的使用这个问题的方法

从通讯录中查询电话号码(由于不同的手机号码存储格式不同,有些包含-,有些还包含空格)

      private string[] projections= new string[] {
               Android.Provider.ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Id,//(可以不加)
               Android.Provider.ContactsContract.CommonDataKinds.Phone.Number,//常用
               Android.Provider.ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName//常用
            };
      string[] to = new string[] { "%" + input.ToString().ToUpper() + "%" };
      string where = "REPLACE(REPLACE(" + Android.Provider.ContactsContract.CommonDataKinds.Phone.Number + ",'-',''),' ','')" + " LIKE ?" + "OR UPPER(" + Android.Provider.ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName + ") LIKE ?";
      
_cursor0 = Application.Context.ContentResolver.Query(
           		Android.Provider.ContactsContract.CommonDataKinds.Phone.ContentUri,
        		projections,
              	where,
            	to, <span style="font-family: Arial, Helvetica, sans-serif;">null);</span>
val cursor: Cursor? = contentResolver.query(
                        data.data!!,
                        projection,
                        null,null,null
                    )
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值