ContentResolver.query()的参数 说明

本文详细介绍了如何使用ContentProvider的query方法进行数据检索。包括如何指定URI、选择要返回的列、设置过滤条件以及排序方式等内容。
public final Cursorquery (Uri uri,String[] projection, String selection, String[] selectionArgs,String sortOrder)
Since: API Level 1

Query the given URI, returning a Cursor over the result set.

For best performance, the caller should follow theseguidelines:

  • Provide an explicit projection, to prevent reading data fromstorage that aren't going to be used.
  • Use question mark parameter markers such as 'phone=?' insteadof explicit values in theselection parameter, so thatqueries that differ only by those values will be recognized as thesame for caching purposes.
Parameters
uriThe URI, using the content:// scheme, for the content toretrieve.
projectionA list of which columns to return. Passing null will return allcolumns, which is inefficient.
selectionA filter declaring which rows to return, formatted as an SQLWHERE clause (excluding the WHERE itself). Passing null will returnall rows for the given URI.
selectionArgsYou may include ?s in selection, which will be replaced by thevalues from selectionArgs, in the order that they appear in theselection. The values will be bound as Strings.
sortOrderHow to order the rows, formatted as an SQL ORDER BY clause(excluding the ORDER BY itself). Passing null will use the defaultsort order, which may be unordered.
Returns
  • A Cursor object, which is positioned before the first entry, ornull

解释一下:假如一条sql语句如下:
select * from anyTable where var='const'
那么anyTable就是uri,*就是projection,selection是“var=?",selectionArgs写成这样:newString[]{'const‘}

至于最后一个就简单了,就是排序方式。


其实 第三个参数是sql语句where部分,如果第三个参数不带 “?”,那么第四个参数 写成 null;










逐句给我解释该log:09-11 00:40:29.719 1000 2709 2709 W System.err: java.lang.IllegalArgumentException: Attempt to launch content provider before system ready 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.server.am.ContentProviderHelper.getContentProviderImpl(ContentProviderHelper.java:452) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.server.am.ContentProviderHelper.getContentProvider(ContentProviderHelper.java:160) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.server.am.ActivityManagerService.getContentProvider(ActivityManagerService.java:7999) 09-11 00:40:29.719 1000 2709 2709 W System.err: at android.app.ActivityThread.acquireProvider(ActivityThread.java:9165) 09-11 00:40:29.719 1000 2709 2709 W System.err: at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:3922) 09-11 00:40:29.719 1000 2709 2709 W System.err: at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:2559) 09-11 00:40:29.719 1000 2709 2709 W System.err: at android.content.ContentResolver.query(ContentResolver.java:1225) 09-11 00:40:29.719 1000 2709 2709 W System.err: at android.content.ContentResolver.query(ContentResolver.java:1168) 09-11 00:40:29.719 1000 2709 2709 W System.err: at android.content.ContentResolver.query(ContentResolver.java:1124) 09-11 00:40:29.719 1000 2709 2709 W System.err: at android.provider.MiuiSettings$SettingsCloudData.getCloudDataSingle(MiuiSettings.java:7257) 09-11 00:40:29.719 1000 2709 2709 W System.err: at android.provider.MiuiSettings$SettingsCloudData.getCloudDataString(MiuiSettings.java:7286) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.server.notification.NotificationManagerServiceImpl.init(NotificationManagerServiceImpl.java:158) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.server.notification.NotificationManagerService.init(NotificationManagerService.java:2868) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.server.notification.NotificationManagerService.onStart(NotificationManagerService.java:3071) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.server.SystemServiceManager.startService(SystemServiceManager.java:289) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.server.SystemServiceManager.startService(SystemServiceManager.java:266) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.server.SystemServer.startOtherServices(SystemServer.java:2584) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.server.SystemServer.run(SystemServer.java:1116) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.server.SystemServer.main(SystemServer.java:780) 09-11 00:40:29.719 1000 2709 2709 W System.err: at java.lang.reflect.Method.invoke(Native Method) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:616) 09-11 00:40:29.719 1000 2709 2709 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1093)
最新发布
09-18
分析一下代码: public void printContacts() { //生成ContentResolver对象 ContentResolver contentResolver = getContentResolver(); // 获得所有的联系人 /*Cursor cursor = contentResolver.query( ContactsContract.Contacts.CONTENT_URI, null, null, null, null); */ //这段代码和上面代码是等价的,使用两种方式获得联系人的Uri Cursor cursor = contentResolver.query(Uri.parse("content://com.android.contacts/contacts"),null,null,null,null); // 循环遍历 if (cursor.moveToFirst()) { int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID); int displayNameColumn = cursor .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); do { // 获得联系人的ID String contactId = cursor.getString(idColumn); // 获得联系人姓名 String displayName = cursor.getString(displayNameColumn); //使用Toast技术显示获得的联系人信息 Toast.makeText(ContentProviderDemo.this, "联系人姓名:" + displayName,Toast.LENGTH_LONG).show(); // 查看联系人有多少个号码,如果没有号码,返回0 int phoneCount = cursor .getInt(cursor .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (phoneCount > 0) { // 获得联系人的电话号码列表 Cursor phoneCursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null); if(phoneCursor.moveToFirst()) { do { //遍历所有的联系人下面所有的电话号码 String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); //使用Toast技术显示获得的号码 Toast.makeText(ContentProviderDemo.this, "联系人电话:"+phoneNumber,Toast.LENGTH_LONG).show(); }while(phoneCursor.moveToNext()); } } } while (cursor.moveToNext()); } } }
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值