我正在尝试从本机数据库中获取联系人列表及其显示名称和电话号码(任何或全部)。有许多方法可以通过对手机数据库的多次查询来获取此信息,但这会带来相当大的开销。
这是我一直在处理的查询,但结果是
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor people = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
int index_id = people.getColumnIndex(ContactsContract.Contacts._ID);
int indexName = people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
String _id = people.getString(index_id);
String name = people.getString(indexName);
String number = people.getString(indexNumber);
// Do work...
} while (people.moveToNext());
这是由此产生的错误。
E/AndroidRuntime(21549): Caused by: java.lang.IllegalArgumentException: Invalid column data1
E/AndroidRuntime(21549): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144)
E/AndroidRuntime(21549): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
E/AndroidRuntime(21549): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:372)
E/AndroidRuntime(21549): at android.content.ContentProviderProxy.query(ContentProviderNative.java:408)
E/AndroidRuntime(21549): at android.content.ContentResolver.query(ContentResolver.java:264)
想法?我相信可能需要连接才能在单个查询中获取所有列。