我正在尝试获取包含所有联系人姓名和生日的 ListView :
在我的 oncreate() 中:
final ListView listview = (ListView) findViewById(R.id.ListView1);
Cursor cursor=getContactsBirthdays();
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,
android.R.layout.two_line_list_item, cursor,
new String[] {ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.START_DATE},
new int[]{android.R.id.text1, android.R.id.text2});
listview.setAdapter(adapter);
我的 getContactsBirthdays() 方法:
private Cursor getContactsBirthdays() {
try {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
// ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE
};
String where = ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, where, null, sortOrder);
} catch (Exception e) {
Log.e("Contact Error", "Error: " + e.getMessage());
return null;
}
}
但我有一个非法参数异常:列'_id'不存在 当我创建适配器时。
谁能解释一下为什么?
最佳答案
来自 the docs for CursorAdapter (SimpleCursorAdapter 扩展):
The Cursor must include a column named "_id" or this class will not work.
关于android - 在android ListView 中获取联系人姓名和生日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19803434/