【发布时间】:2012-12-06 18:13:34
【问题描述】:
我正在使用以下代码从 android 联系人中检索名字和姓氏。DISPLAY_NAME 返回联系人的姓名,而 firstname 和 lastname 分别返回 1 和 null。以下是代码。
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()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String firstname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String lastname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
System.out.println("firstname and lastname" + firstname+ " "+lastname);
phoneNo = phoneNo.replaceAll("\\D", "");
names.add(name);
phnno.add(phoneNo);
}
pCur.close();
}
}
}
当我将 cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI 行更改为 cr.query(ContactsContract.Data.CONTENT_URI) 时,我得到的日志为
非常感谢您的建议。在此先感谢
【问题讨论】:
-
这些可能是链接的联系人,并且可能没有附加姓名吗?
-
我在编写代码之前已经亲自添加了它们。它们都附有名称
-
您找到解决方案了吗?我遇到了同样的问题