如何在邮件列表中显示联系人姓名或联系人的display_name



如果数字在循环中给定,我如何在Listview中显示以下数字的名称。void FetchAllMessages(({msgList=新排列列表<>((;

Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
if (cursor.moveToFirst()) {
do {
String from = cursor.getString(cursor.getColumnIndex("address"));
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(from));
Cursor phones = getContentResolver().query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if(phones.moveToFirst()){
contactname = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
msgList.add(contactname);
}else{
msgList.add(from);
}
} while (cursor.moveToNext());
}
}

在我的android中没有显示,它继续崩溃。非常感谢您的帮助。

您似乎没有为无联系人检查编写代码。试试这个代码

ContentResolver cr = cntx.getApplicationContext().getContentResolver();
//Query to get contact name
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
// If data data found in contacts
ArrayList<String> msgList = new ArrayList<String>();
if (cur.getCount() > 0) {
int k = 0;
String name = "";
while (cur.moveToNext()) {
String id = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//Check contact have phone number
if (Integer
.parseInt(cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//Create query to get phone number by contact id
Cursor pCur = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?",
new String[]{id},
null);
int j = 0;
while (pCur
.moveToNext()) {
// Sometimes get multiple data
if (j == 0) {
// Get Phone number
phoneNumber = "" + pCur.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (phoneNumber.startsWith("+91")) {
phoneNumber = phoneNumber.substring(3, phoneNumber.length());
}
phoneNumber = phoneNumber.replaceAll("\s", "");
if (phoneNumber.length() == 10) {
// Add contacts names to arrayList
msgList.add(name.toString());
}
j++;
k++;
}
}  // End while loop
pCur.close();
} // End if
}  // End while loop
} // End Cursor value check
cur.close();

最新更新