Display the Contacts in sorting order ContactsContract.Contacts of Content Resolver

user1862773 picture user1862773 · Dec 11, 2012 · Viewed 22.3k times · Source

My intention is to display the contacts in sorting order using content resolver in android.

For that I'm writing:

Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[] { id }, null);

It needs that the last parameter in query method should not be null for sorting the elements by Name. Which part of code I have to replace the null parameter to achieve sorting by name?

Answer

ρяσѕρєя K picture ρяσѕρєя K · Dec 11, 2012

To sort result according to name use Phone.DISPLAY_NAME constant with ASC as last parameter to query method. do it as:

  Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                   null, 
                   ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",
                   new String[] { id },
                   ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");