自学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. |
第一个参数代表你要查什么,是查联系人,还是查书签,还是查字典?
第二个参数代表你要查项目,比如说你第一个参数选了联系人,那这个参数告诉方法你查的是联系人的姓名?还是电话号码?还是Email?(可以查多个,projection本来就是数组嘛。)
第三个参数代表你要得到的具体内容,比如说查联系人,那你手机里用100个联系人,你只要电话号码为110的和112的两个人的信息。那你就填“phone=?”。(那110和112不用填?别急,看下面。)
第四个参数就是匹配项,按照上面的要求,你要找110和112,这里就填["110","112"]。
第五个是排序标准,你是要先显示110呢?还是先112呢?就在这里决定啦。
以上是个人见解,欢迎讨论。