【发布时间】:2015-10-12 07:54:28
【问题描述】:
我正在尝试使用此代码检索联系人号码和联系人姓名。我得到了数字,但名称列表返回 null
ContentResolver cr = this.getContentResolver(); //Activity/Application android.content.Context
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cursor.moveToFirst())
{
List<String> ContactNames = new ArrayList<String>();
do
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
if(Integer.parseInt(cursor.getString(cursor.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 contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactNames = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
ContactNames.add(contactNames);
break;
}
pCur.close();
}
} while (cursor.moveToNext()) ;
}
我不知道我错过了什么。请帮忙
【问题讨论】:
-
"字符串 contactNames = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));"你需要 CONTACT_ID 做什么?
-
检查这个所以问题它有你需要的一切stackoverflow.com/questions/6152442/how-to-get-contact-email-id
-
@KaranMer 这将非常慢,如果与数百个联系人一起使用可能需要一分钟或更长时间
-
@pskink 抱歉我更新了它.. 实际上我正在尝试它是否返回任何内容
-
@KaranMer 和这个比较一下:stackoverflow.com/a/26820544/2252830
标签: android android-listview android-contacts