Piolo Opaw
Piolo Opaw

Reputation: 1511

best way to create sort order in the Cursor in android?

Is creating different cursor for creating sort orders and distribute it in an model clases for alternating sorting style in an listview in android good practice?

titleCursor = context.getContentResolver().query(sourceUri, projection,
                null, null, orderByTitle);

timeCursor = context.getContentResolver().query(sourceUri, projection,
                null, null, orderByTime);

dateCursor = context.getContentResolver().query(sourceUri, projection,
                null, null, orderByDate);

and values of its cursor will be destributed to each models to get the cursor for sorting for alternating sorting in listview? is it a good practice?

Upvotes: 1

Views: 2988

Answers (2)

DSS
DSS

Reputation: 7259

Instead of creating a different cursor for each sort, you could maintain a string value or some integer value and based on the value inside an if condition simply change the sort parameter like:

if(value.equals("time")) {
    cursor = context.getContentResolver().query(sourceUri, projection,
            null, null, orderByTime);

}else if(value.equals("title")) {
    cursor = context.getContentResolver().query(sourceUri, projection,
            null, null, orderByTitle);
}

and so on.

Upvotes: 0

Peter Birdsall
Peter Birdsall

Reputation: 3425

You don't have to create a new cursor. You can just change the sort value for the query, if all other things remain constant. That's why it's a parameter, so you alter it's value upon execution of the query.

Upvotes: 1

Related Questions