按联系人姓名排序联系人

时间:2015-08-29 08:48:00

标签: android sorting android-contacts

我想在提取联系人时对联系人进行排序,然后按照联系人姓名给出声明,但没有任何作用。

这是我的代码

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

1 个答案:

答案 0 :(得分: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"));
    }
}

但是,如果您坚持使用地图,则无需按排序顺序获取联系人。在getter方法中将它们放在HashMap(键是数字,值是名称)中,然后在读取键和值时,将所有内容从HashMap传输到TreeMap:

treeMap.putAll(hashMap);

现在您可以按排序顺序打印内容。