Search code examples
androidsortingandroid-cursorandroid-cursorloader

CursorLoader Sort Order and updates


I have a gallery app using a CursorLoader that allows sorting of images by certain metadata. Sort is defined when the CursorLoader is created.

            // Returns a new CursorLoader
            return new CursorLoader(
                    this,                   // Parent activity context
                    Meta.Data.CONTENT_URI,  // Table to query
                    projection,             // Projection to return
                    selection,              // No selection clause
                    selectionArgs,          // No selection arguments
                    sort                    // Default sort order
            );

If the user selects an images they are taken to a viewer. They have the opportunity to manipulate the metadata. This causes an automatic resort, which can be confusing. The selected image may move from the middle to the end based on the sorting and the user won't realize this, they'll simply be unable to swipe when they thought they could.

Is there any way to sort a CursorLoader initially, then have it maintain that _id order, henceforth, even if the data the sort is based on changes?


Edit: Possible Solution

Only thing I can think of is to maintain an 'order' column which the viewer activity will sort on and a 'select' column which will handle the selection.

Before launching the viewer i'll iterate through the cursor inserting an 'order' counter. I'll then bulk insert. The viewer activity should maintain the previous ordering regardless of changes to the metadata within the viewer.

Two extra columns feels like a bit of a hack, but it should work. The only other thing I can think of is to manage a separate custom datasource for the viewer, but that has it's own issues.

Hopefully there's a better way to do this.


Solution

  • The main reason for using the CursorLoader was to leverage a base activity that could ensure the same dataset was used between activities without additional coding. However, since my second activity needed to "lock" onto the dataset of the prior I realized this was a poor use of CursorLoader as it's main strength is staying in sync with changes to the database. I actually wanted to ignore these changes in the second activity. Instead I created a custom dataset manager for the second app.

    (Not sure this is an really a relevant question any longer, but since someone up-voted it, I'll share my final conclusion)