I have an implementation of CursorAdapter
which I am using with a Loader
through a ListFragment
. This works fine except for the problem below. It contains the following code. Each row contains a checkbox where the user can select the items. There is a delete button on actionbar that allows the user to delete the selected items. This happens through my implementation of ContentProvider
.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
//some logic
this.cursor = cursor; //class variable assignment
}
void deleteStuff() {
if (cursor == null) {
return;
}
if (checkedItems == null || checkedItems.size()==0) { //A Sparse boolean array which saves the positions of items the user selected
return;
}
final SparseIntArray checkedKeys = new SparseIntArray(); //positions of selected items
final SparseLongArray checkedIds = new SparseLongArray(); //ids of the items at the resp keys
for (int i = 0; i < checkedItems.size(); i++) {
final int checkedItemKey = checkedItems.keyAt(i);
checkedKeys.append(i, checkedItemKey);
cursor.moveToPosition(checkedItemKey); //the line at which it fails!!!!!!!!
checkedItemIds.append(checkedItemKey, Long.parseLong(c.getString(0)));
}
for (int i = 0; i < checkedItems.size(); i++) {
myContentProvider.delete(uri, selection, args); //not putting the code for these 3 variables as not required
}
}
Here is the corresponding function in the ContentProvider
:
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int uriType = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = sqlitehelper.getWritableDatabase();
int rowsDeleted = sqlDB.delete(TJItemTable.TABLE_NAME, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return rowsDeleted;
}
When I select multiple items and delete, the first time it works fine and does what it is supposed to do. However, now if I select some more item or items, and again click on delete, it fails at line XX.
The error I get in LogCat is java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT id as _id, name, value FROM stufftable WHERE (value=?)
. The line at which it fails is: cursor.moveToPosition(checkedItemKey)
I did look at this, and understand that somehow I am leaving the cursor in a closed or an inconsistent state. However, I am not able to think of anything that could solve my issue. What am I doing wrong?
Note: I am calling getContext().getContentResolver().notifyChange(uri, null)
in my ContentProvider.delete()
which along with the Loader
, I thought, would notify the cursor. Also I tried putting this.notifyDataSetChanged()
at the end without any luck.