Search code examples
javaandroidandroid-contactsandroid-cursorloader

CursorLoader selection


I have managed to view a list of contacts in my android app, the list is unsorted so I would like to sort it from A to Z.

My CursorLoader look as follows:

private static final String[] PROJECTION = {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.LOOKUP_KEY,
        Build.VERSION.SDK_INT
                >=Build.VERSION_CODES.HONEYCOMB ?
                ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
                ContactsContract.Contacts.DISPLAY_NAME
};

private static final String SELECTION =null;
private String[] mSelectionArgs ={ };

public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {

    return new CursorLoader {
            getApplicationContext(),
            ContactsContract.Contacts.CONTENT_URI,
            PROJECTION,
            SELECTION,
            mSelectionArgs,
            null
    }
}

Please advise parameters that the above selection variables must take to for example sort the contacts alphabetically.


Solution

  • Change the last constructor parameter to "data ASC":

    new CursorLoader(getActivity(), 
            ContactsContract.Contacts.CONTENT_URI,
            PROJECTION,
            SELECTION,
            mSelectionArgs,
            "data ASC");
    

    Where data is the column that should be alphabetically sorted.