에서 ListFragment
까지 사용하는 CursorAdapter
구현이 있습니다. 아래의 문제를 제외하고이 작동합니다. 여기에는 다음 코드가 포함되어 있습니다. 각 행에는 사용자가 항목을 선택할 수있는 확인란이 있습니다. 삭제 버튼이있어 사용자가 선택한 항목을 삭제할 수 있습니다. 이것은 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)
을 넣고 Loader
과 함께 커서에 알릴 것입니다. 또한 행운없이 끝에 this.notifyDataSetChanged()
넣어려고했는데.
게시 logcat 및 완전한 ContentProvider – pskink