2015-08-29 193 views
0

我想在聯繫人被提取時對聯繫人進行排序,並且我通過聯繫人姓名向聯繫人發出了訂單,但沒有任何結果。通過聯繫人姓名對聯繫人進行排序

這是我的代碼

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; 
} 
+0

看看[鏈接](http://androhub.com/android-read-contacts-using-content-provider/)。 –

回答

0

TreeMap中是根據其鍵爲默認的自然順序進行排序地圖。您嘗試根據電話號碼對聯繫人進行排序。因此,如果要工作,你的代碼,你需要改變這一行:

result.put(phoneNo, name); 

這一行:

result.put(name, phoneNo); 

此外,您還可以得到排序的聯繫人,而無需使用TreeMap的如下:

ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 


public void retrieveContactList() { 

    Cursor phones = null; 

    try { 
     phones = activity.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 
     while (phones.moveToNext()) 
     { 
      String _number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("\\s+", ""); 
      String _name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 

      HashMap<String, String> contact = new HashMap<String, String>(); 
      contact.put("NUMBER", _number); 
      contact.put("NAME", _name); 
      contactList.add(contact); 
     } 

     phones.close(); 

    } catch (Exception e) {} 

    finally { 
     if(phones != null){ 
      phones.close(); 
     } 
    } 
} 

public void printContacts() { 

    for (int i = 0; i < contactList.size(); i++) { 
     Log.v(contactList.get(i).get("NAME"), contactList.get(i).get("NUMBER")); 
    } 
} 

但是,如果您堅持使用地圖,則不需要按排序順序獲取聯繫人。把它們放在HashMap中(關鍵是數量和價值名稱)在您的getter方法,然後在讀取鍵和值,從HashMap中傳輸的所有內容TreeMap中:

treeMap.putAll(hashMap); 

現在你可以在打印內容排序順序。

+0

由於聯繫人重複,我無法通過聯繫人姓名進入地圖。它取代了另一個。 – Alireza

+0

您需要使用ArrayList >作爲數據結構來存儲聯繫人。我編輯了我的答案,請你檢查一下嗎? –

相關問題