Search code examples
cursorandroid-contentproviderandroid-cursoradapter

How to sort data using Cursor with Content Provider and Cursor Adapter?


I have a Content Provider and a Cursor Adapter. One of the columns in my table is an int. I want to order my table by this column, so the Adapter will show the data accordingly. I tried to do this: In the Activity:

Cursor c = getContentResolver().query(
                PetContract.Dogs.CONTENT_URI,
                null, null, null,
                PetContract.Dogs.DATE + " ASC");

        testAdapter = new TestAdapter(this, c);
        c.close();
        list.setAdapter(testAdapter);
        getLoaderManager().initLoader(1, null, this);
        registerForContextMenu(list);

In the Adapter constructor:

public TestAdapter(Context context, Cursor c) {
        super(context, c);
    }

But it didn't work...


Solution

  • Figured it out. In my Activity:

     @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            return new CursorLoader(this,
                    PetContract.Dogs.CONTENT_URI,
                    null,
                    null,
                    null,
                    PetContract.Dogs.DATE + " collate localized asc"
            );
        }