ContentResolver.query()的用法

本文深入探讨了Android应用开发中ContentResolver组件的作用,包括其参数详解、使用场景和最佳实践,帮助开发者更高效地进行数据检索与操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自学Android 看ContentResolver的时候真是云里雾里。查了下Google的API手册,如下:

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 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
uri The URI, using the content:// scheme, for the content to retrieve.
projection A list of which columns to return. Passing null will return all columns, which is inefficient.
selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.
selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.
sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
总结一下,query一共有五个参数。

第一个参数代表你要查什么,是查联系人,还是查书签,还是查字典?

第二个参数代表你要查项目,比如说你第一个参数选了联系人,那这个参数告诉方法你查的是联系人的姓名?还是电话号码?还是Email?(可以查多个,projection本来就是数组嘛。)

第三个参数代表你要得到的具体内容,比如说查联系人,那你手机里用100个联系人,你只要电话号码为110的和112的两个人的信息。那你就填“phone=?”。(那110和112不用填?别急,看下面。)

第四个参数就是匹配项,按照上面的要求,你要找110和112,这里就填["110","112"]。

第五个是排序标准,你是要先显示110呢?还是先112呢?就在这里决定啦。

以上是个人见解,欢迎讨论。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值