Java – How do I sort phone contacts in ListView?

How do I sort phone contacts in ListView?… here is a solution to the problem.

How do I sort phone contacts in ListView?

I’ve created a ListView :

http://www.vogella.com/articles/AndroidListView/article.html

In that link, I’ve implemented it

13> Select multiple items in the ListView

My ListView works fine. I customized the ListView by populating it with phone contacts with the following code:

private List<Model> getModel()        
{                  
    List<Model> list = new ArrayList<Model>();    
    ContentResolver cr = 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)
               {
                     This inner cursor is for contacts that have multiple numbers.
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                    while (pCur.moveToNext())
                    {
                         list.add(get(name));
                    }
                    pCur.close();
               }
         }
    }
    cur.close();
    return list;
}

The contact’s name is now displayed, but it is not sorted. How do I sort it?

Seek help!
Thanks in advance!

Solution

Let’s look at this statement.

List<Model> list = new ArrayList<Model>();    

This is an ArrayList object, parameterized as a Model type. Now, in the Model class you created, you can simply use the Comparable interface. To do this, you add:

implements Comparable<Model>

to the end of your Model class header.

You will then be forced to add an implementation for the method named compareTo. This is how you compare things.

After adding this implementation, you can call Collections.sort() and pass an ArrayList object list to it. This will sort the list for you.

I think it’s a little deeper

So, the first thing to do is go to your Model class, which currently has a title like this:

public class Model

All you want to do is change it to something like this:

public class Model implements Comparable<Model>

What does that mean?

This means that you have agreed to provide code for the Comparable interface. You now need to write a method compareTo in your Model class. Something like this:

@Override
public int compareTo(Object arg0) {
    At this point, you put your comparison code in.
}

Your comparison code

If you want to sort them alphabetically, you can use the compareTo method in the String object. First, you need to convert arg0 to a String object that you can use:

String otherName = ((Member)arg0).getName();

Then you can simply return the value of the String compareTo method:

return otherName.compareTo(this.name);

Related Problems and Solutions