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;
}