我使用下面的代碼從android聯繫人中檢索姓和名.DISPLAY_NAME在姓和名返回1時返回聯繫人的姓名,下面是代碼。從android聯繫人中檢索名字和姓氏導致'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我得到的日誌作爲
建議高度提前appreciated.Thanks
這可能是鏈接聯繫人,也許沒有附加名稱? – breadbin
我親自添加了他們,就在我編寫代碼之前。他們都附上了姓名 –
您是否找到解決方案?我遇到同樣的問題 –