我有一個CursorAdapter
的實現,我正在使用Loader
到ListFragment
。這工作正常,除了下面的問題。它包含以下代碼。每行包含一個複選框,用戶可以在其中選擇項目。在操作欄上有一個刪除按鈕,允許用戶刪除所選項目。這發生在我執行ContentProvider
。在CursorAdapter中刪除項目
@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
}
}
這裏是在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;
}
當我選擇多個項目,並刪除,它工作正常,做什麼是應該做的第一次。但是,現在如果我選擇更多項目或項目,並再次單擊刪除,它將在XX行失敗。
我收到的錯誤LogCat是java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT id as _id, name, value FROM stufftable WHERE (value=?)
。它失敗的路線是:cursor.moveToPosition(checkedItemKey)
我確實看過this,並明白我在離開光標時處於關閉狀態或不一致狀態。但是,我無法想到任何可以解決我的問題的東西。我究竟做錯了什麼?
注意:我打電話getContext().getContentResolver().notifyChange(uri, null)
在我的ContentProvider.delete()
,我認爲它會與Loader
一起通知遊標。我還試着把this.notifyDataSetChanged()
放在最後,沒有任何運氣。
發佈logcat並完成ContentProvider – pskink