如何匹配Android联系人列表中的名称和电话与Collcations

时间:2014-05-09 03:51:11

标签: android listview contact

我在使用我正在建立的应用程序提取数据时遇到问题。 我试图拉我的应用程序联系人列表并按字母顺序A-B-C(名称和PhoneNumber)设置它 当我尝试 Collections.sort(名称)时,问题就出现了 在我进行了搭配之后,该名称与电话号码不匹配。 我该怎么办订单? 谢谢!

    private void GetContact() {
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER, BaseColumns._ID };
    Cursor cursor =getContentResolver().query(uri, projection, null, null, null);

    int id = cursor.getColumnIndex(BaseColumns._ID);
    int indexName = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexPhone = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    cursor.moveToFirst();
    do{
        String _id = cursor.getString(id);
        String name = cursor.getString(indexName);
        String phone = cursor.getString(indexPhone);

        arrayName.add(name);
        arrayPhone.add(phone);
    }while (cursor.moveToNext());
    Collections.sort(arrayName);
}

3 个答案:

答案 0 :(得分:1)

使用

String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
Cursor cursor = contentResolver
                .query(uri, projection, null, null, "UPPER("+ DISPLAY_NAME + ") ASC");

它将通过忽略姓名的情况给出按名称排序的联系人。

答案 1 :(得分:0)

我假设您在显示已排序的联系人姓名的相应数量时遇到问题。排序后的名称有不同的数字??你可以做类似下面的事情,这只是一个给你想法的例子。修改你想要的方式。

List<Empl> list = new ArrayList<Empl>();
     list.add(new Empl("Harry", 123));
        list.add(new Empl("Ram",3000));
        list.add(new Empl("John",6000));
        list.add(new Empl("Crish",2000));
        list.add(new Empl("Tom",2400));

        Collections.sort(list, new MyNumberComp());
        System.out.println("Sorted list entries: ");
        for(Empl e:list){
            System.out.println(e);  // Instead of printing set the list to Listview.
        }

class MyNumberComp implements Comparator<Empl>{

    public int compare(Empl s1, Empl s2) {
          if (s1.getName().toString() == null) {
              return (s2.getName().toString() == null) ? 0 : +1;
          } else {
              return (s2.getName().toString() == null) ? -1 : s1.getName().toString().compareTo(s2.getName().toString());
          }
      }
}

class Empl{

    private String name;
    private int number;

    public Empl(String n, int s){
        this.name = n;
        this.number = s;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }


}

答案 2 :(得分:0)

尝试这种方式希望这可以帮助你

private ArrayList<HashMap<String, String>> GetContact() {

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

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, BaseColumns._ID };
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {

        int id = cursor.getColumnIndex(BaseColumns._ID);
        int indexName = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexPhone = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        do {
            HashMap<String, String> item = new HashMap<String, String>();
            item.put("id", cursor.getString(id));
            item.put("phone", cursor.getString(indexPhone));
            item.put("name", cursor.getString(indexName));
            contacts.add(item);
        } while (cursor.moveToNext());
    }

    Collections.sort(contacts, new Comparator<HashMap<String, String>>() {

        @Override
        public int compare(HashMap<String, String> lhs, HashMap<String, String> rhs) {
            return lhs.get("name").toLowerCase().compareTo(rhs.get("name").toLowerCase());
        }
    });
    return contacts;
}