1

in my application I am displaying a list of items queried from the database with the use of Loader Manager and Content Provider. The results are attached to a SimpleCursorAdapter and then displayed on the List View. I would like to be able to load, let's say up to 20 items at first and when scrolling to the end of that list, load up additional 20 items. Up to now, I used similar approach as here:

How to update listview whose data was queried from database through SimpleCursorAdapter?

i.e. limiting query results to 20 and increasing the number appropriately in the OnScrollListener and restarting the loader with new instructions. The problem is with this statement:

incomeAdapter.swapCursor(data);

It completely resets adapter's data, therefore only the second batch of items are displayed on the list. Is there any way to simply load additional items on top of existing ones? Thanks

EDIT: Merging cursors like this:

if (incomeAdapter.getCursor() != null){
        Log.v("Income Activity", "current adapter");
        Cursor oldData = incomeAdapter.getCursor();
        MergeCursor merge = new MergeCursor(new Cursor[] {oldData, data});
        incomeAdapter.swapCursor(merge);
    }
    else {
        Log.v("Income Activity", "no current adapter");
        incomeAdapter.swapCursor(data);
    }

throws an exception:

07-13 16:56:15.455: E/AndroidRuntime(5665): FATAL EXCEPTION: main
07-13 16:56:15.455: E/AndroidRuntime(5665): android.database.StaleDataException:     
Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated
prior to calling this method.  

EDIT2: As per request, the code where data is requested:

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    orderBy = sortOrder + " LIMIT " + limitSkip + "," + limitCount;

    CursorLoader cursorLoader = new CursorLoader(this,
        DatabaseProvider.CONTENT_URI1, PROJECTION, null, null, orderBy);

    return cursorLoader;
}
Community
  • 1
  • 1
tomz
  • 341
  • 3
  • 14

1 Answers1

0

You need to use the previous cursor: incomeAdapter.getCursor() and merge it with the new data.

MergeCursor comes to your aid there:

Cursor oldData = incomeAdapter.getCursor();
MergeCursor merge = new MergeCursor(new Cursor[] {oldData, data});
incomeAdapter.swapCursor(merge);

EDIT:

You are using a CursorLoader. You don't need to re-query the database or LIMIT your queries. The loader does all that for you when specific data is requested.

Android Docs: Loaders

Simas
  • 43,548
  • 10
  • 88
  • 116
  • Tried similar solution (see my edited post) and it throws a StaleDataException. – tomz Jul 13 '14 at 16:01
  • ok so what happens when I have a 100+ records matching my query? Are they all added to the list at once? – tomz Jul 13 '14 at 16:29
  • Have you managed to figure out the reason behind that StaleDataException? I need to do the same thing and apart from this issue, everything is working out as it should. – Bogdan Zurac Jan 16 '16 at 15:03
  • Opened up a question solely related to this issue over at http://stackoverflow.com/questions/34828657/using-pagination-with-cursorloader-and-mergecursor-closes-old-cursors. Sorry for 2 comments, but apparently SO doesn't allow editing after 5 minutes, ffs. – Bogdan Zurac Jan 16 '16 at 15:25