0

我有一個CursorAdapter的實現,我正在使用LoaderListFragment。這工作正常,除了下面的問題。它包含以下代碼。每行包含一個複選框,用戶可以在其中選擇項目。在操作欄上有一個刪除按鈕,允許用戶刪除所選項目。這發生在我執行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行失敗。

我收到的錯誤LogCatjava.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()放在最後,沒有任何運氣。

+0

發佈logcat並完成ContentProvider – pskink

回答

0

更新:我終於做了這個更簡單的方法。我用checkBox.setOnCheckedChangeListener()內部的位置在bindView()中填充checkedItems。我改變了這個以包含被刪除項目的id而不是獲取位置,然後使用位置爲:)的光標。所以在delete()中,我已經準備好刪除Ids,並且我不必在這裏混淆光標。這符合我的願望。我希望我早點想到這一點,那麼我就不必在這裏發佈問題。我希望這可以幫助別人!

0

每次輸入時,都會在deleteStuff()中創建一個新的光標,而不是使用newView中的光標,該光標搜索要刪除的條目。 (這應該工作)

你也可以嘗試從bindView使用遊標,但我不知道這是否會工作。

+0

我認爲這不是一個好主意。與'Loader/LoaderManager'一起使用'CursorAdapter'的一點是我不必自己管理光標:) – rgamber

+0

它是用於加載的,你也可以在deleteStuff結尾處嘗試cursor.requery() ),但這種方法似乎已被棄用。讓我知道你是否嘗試過它,如果它工作。同時我會想到一個更好的方法。 –

+0

你也可以嘗試從bindView(View view,Context context,Cursor cursor)中使用遊標。我認爲bindView使用刷新的遊標。 –