How to sort the CursorLoader results?

Viewed 10744

I use CursorLoader to query a result, which is not the order that I want to show in the ListFramgenet. How to sort it ?

I use this to set the adapter:

    mAdapter = new SimpleCursorAdapter(getActivity(),
            android.R.layout.simple_list_item_2, null,
            new String[] { "name", "distance"},
            new int[] { android.R.id.text1, android.R.id.text2 }, 0);
    setListAdapter(mAdapter);

    // Start out with a progress indicator.
    setListShown(false);

    // Prepare the loader.  Either re-connect with an existing one,
    // or start a new one.
    getLoaderManager().initLoader(0, null,  this);

Create loader :

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

    return new CursorLoader(getActivity(), 
            Uri.withAppendedPath(TraceTable.CONTENT_URI, "latest"),
            MEMBERS_PROJECTION,
            null,
            null,
            null);        

}


public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.changeCursor(data);

    // The list should now be shown.
    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}

Well, there are latitude and longitude the queried results. I want to calculate the distance between my location and these results. and sort by distance asc.

How to sort it? Any answer will be appricated

4 Answers

Just add ContactsContract.Contacts.SORT_KEY_PRIMARY.

Related