How to specify the sort order for the query to a content provider
Asked Answered
D

2

25

I am trying to load all items from a Sqlite Table in Android and want to order the result.

How do I specify the order for the returned cursor?

I query the ContentProvider through a CursorLoader the following way:

new CursorLoader(context, RepresentativeEntityContentProvider.CONTENT_URI, null, null,
            null, null);
Ddene answered 29/12, 2014 at 9:42 Comment(2)
#20686915Haricot
additional to accepted answer: its possible to sort for more then one column, e.g. "column_name1 ASC, column_name2 DESC, column_nameN ASC" where DESC means descending.Promethean
T
46

try below code change COLUMN_NAME with actual column name on which you wants to sort.

new CursorLoader(context, RepresentativeEntityContentProvider.CONTENT_URI, null, null,
            null, "COLUMN_NAME ASC");
Thursby answered 29/12, 2014 at 9:48 Comment(1)
for example if you're looking for images and you want to see the latest first - add: "date_modified DESC"Oleate
R
12
new CursorLoader(context, RepresentativeEntityContentProvider.CONTENT_URI, null, null,
            null, "column_name ASC");

or

new CursorLoader(context, RepresentativeEntityContentProvider.CONTENT_URI, null, null,
                null, "column_name DESC")

;

Respecting answered 29/12, 2014 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.