获取和显示联系人
好吧,我一直在四处寻找,但找不到实际显示联系人列表的具体方法。我找到了诸如 high pass 之类的教程,但它从来没有实际上讨论了如何显示它们,只是如何获取它们。我只是想在 listView 或类似的东西中显示联系人列表。我知道它一定比我想象的要简单得多,因为这似乎是一件常见的事情。具体来说,我想要的只是联系人的姓名和电话号码。我已经设置了查询,我得到了上面提到的教程,我认为它是正确的:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
}
pCur.close();
}
}
}
Well, I've been looking around and can't find a specific way to actually DISPLAY the contacts list. I've found tutorials such as higher pass's but it never actually discusses how to display them, just how to get them. I simply want to display the contacts list in a listView or something similar. I know it has to be a lot more simple than I'm making it out to be, because it seems to be a common thing. Specifically, all I want is the contact's name and phone numbers. I have my query set up, which I got the above mentioned tutorial, and I think it's right:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
}
pCur.close();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此有一个内置活动:
如果您使用
startActivityForResult()
,您甚至可以取回用户选择的联系人。如果这不是您想要的,您希望在可能的情况下减少到单个查询,然后为您的
ListView
创建一个Adapter
。 这是一个示例项目,它显示了查询联系人以填充ListView
,显示联系人姓名、姓名和电话号码,或者姓名和电子邮件地址。这变得有点复杂,因为示例显示了 Android 1.6 及更低版本和 Android 2.0 及更高版本的联系人 API。There is a built-in activity for this:
If you use
startActivityForResult()
, you can even get the user's chosen contact back.If that's not what you're looking for, you want to cut back to a single query if possible, then create an
Adapter
for yourListView
. Here is a sample project that shows querying the contacts to populate aListView
, showing either the contact's name, name and phone number, or name and email address. This gets a bit more complicated because the sample shows both the Android 1.6-and-lower and Android 2.0-and-higher contacts APIs.