Question

I have a problem pulling data using an app I am buiding. I tried to pull my app Contact List and set it in Alphabetic order A-B-C (name and PhoneNumber) and the problem start when I tried to do the Collections.sort(name) after I did the collocation, the name doesn't match the phone number. How should I do the order? Thanks!

    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);
}
Was it helpful?

Solution

Use

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

It will give u contacts ordered by its name with ignoring case of name.

OTHER TIPS

I assume you are having problem in displaying the respective numbers of sorted contacts name. After sorting the names have different numbers??. You can do something like below, this is just an example to give you idea. Modify how you want it.

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


}

try this way hope this helps you

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top