Display the Contacts in sorting order ContactsContract.Contacts of Content Resolver
Asked Answered
F

5

18

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?

Fresh answered 11/12, 2012 at 6:24 Comment(0)
A
40

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");
Allembracing answered 11/12, 2012 at 6:36 Comment(3)
Do you happen to know where the list of different values of the constants for sorting can be found. I assume "DEC" may be the other way around but what about other forms of sorting?Thermidor
what is {id} here?Housum
@Thermidor it's "DESC" for descendingTakeshi
J
15

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

Cursor cursor = getContentResolver().query(
    ContactsContract.Contacts.CONTENT_URI,
    null, null, null,
    ContactsContract.Contacts.SORT_KEY_PRIMARY + " ASC");
Jolie answered 16/1, 2015 at 15:52 Comment(2)
@Hamidreza are you sure? SORT_KEY_PRIMARY has been introduced in API 11…Siloam
Great solution buddyUnqualified
D
14

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

ContentResolver cr = getContentResolver();

Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        null, null,  "upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
Delay answered 30/6, 2014 at 11:52 Comment(1)
Does a standard sorting method (not using upper) give weight to uppercase letters? Like will names in all caps be BEFORE names in lowercase? If so, is that the reason behind your "upper(" line of code? If not, what does this do?Montane
C
1

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.

Coagulant answered 7/7, 2019 at 18:53 Comment(0)
C
0

You can use the Desc string using these lines to sort out to see the recent contact

    android.database.Cursor cursor = getContentResolver()
            .query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null, null, null,
                    ContactsContract.Data.CONTACT_LAST_UPDATED_TIMESTAMP +" DESC");
Crifasi answered 22/6, 2022 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.