2015-08-29 143 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 >作为数据结构来存储联系人。我编辑了我的答案,请你检查一下吗? –

相关问题