Search code examples
androidandroid-sqliteandroid-cursorloader

Distinct Query For CursorLoader


I am using CursorLoader to populate my listView. But I need to get distinct result set. But CursorLoader has no constructor to set distinct parameter.

Any Suggestions?


Solution

  • Yes!

    In the CursorLoader constructor you specify a URI. Part of that URI is the ContentProvider Authority. The rest, though, is up to you! So create a virtual table!

    If you have a table "songbirds", perhaps your URI is:

    content://my.content.provider/songbirds
    

    Simply teach your ContentProvider (add one more clause to your UriMatcher) to recognize the URI:

    content://my.content.provider/distinctSongbirds
    

    Process the "distinct" URI exactly as you do the non-distinct one, except add a "distinct" param to the actual DB query, when you make it.