Skip to content Skip to sidebar Skip to footer

Display The Contacts In Sorting Order Contactscontract.contacts Of Content Resolver

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.CONT

Solution 1:

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

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

Solution 2:

You can use Upper() to sort for both lower as well as upper case contact name.

ContentResolvercr= getContentResolver();

Cursorcur= cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        null, null,  "upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");

Solution 3:

It would be better to use SORT_KEY_PRIMARY or SORT_KEY_ALTERNATIVE on API level 11 and later.

Cursorcursor= getContentResolver().query(
    ContactsContract.Contacts.CONTENT_URI,
    null, null, null,
    ContactsContract.Contacts.SORT_KEY_PRIMARY + " ASC");

Solution 4:

The ContentResolver.query() method takes many arguments but to sort the content provider records, you have to edit the last argument of this method.

enter image description here

It should be like this:

Cursor cursor=getContentProvider().query(.......,"DISPLAY_NAME ASC")

This will arrange the contacts in Ascending order of their name.

Note: This argument should be in a String datatype.

Post a Comment for "Display The Contacts In Sorting Order Contactscontract.contacts Of Content Resolver"