我想在聯繫人被提取時對聯繫人進行排序,並且我通過聯繫人姓名向聯繫人發出了訂單,但沒有任何結果。通過聯繫人姓名對聯繫人進行排序
這是我的代碼
public static TreeMap<String, String> getContactsMobileNumbersAndNames(Context context) {
TreeMap<String, String> result = new TreeMap<>();
ContentResolver cr = context.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()) {
int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
result.put(phoneNo, name);
}
}
pCur.close();
}
}
}
cur.close();
return result;
}
看看[鏈接](http://androhub.com/android-read-contacts-using-content-provider/)。 –