gpt4 book ai didi

android - 在android ListView 中获取联系人姓名和生日

转载 作者:行者123 更新时间:2023-11-30 03:17:20 26 4
gpt4 key购买 nike

我正在尝试获取包含所有联系人姓名和生日的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com