0

에서 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 줄에서 오류가 발생합니다.

오류가 발생하면 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)을 넣고 Loader과 함께 커서에 알릴 것입니다. 또한 행운없이 끝에 this.notifyDataSetChanged() 넣어려고했는데.

+0

게시 logcat 및 완전한 ContentProvider – pskink

답변

0

업데이트 : 나는 마침내 훨씬 더 간단한 방법으로이 작업을 수행했습니다. checkBox.setOnCheckedChangeListener() 내부의 bindView() 내부 위치로 checkedItems를 채 웠습니다. 위치를 가져 오는 대신 삭제할 항목의 ID를 포함하도록 변경 한 다음 위치에 커서를 사용하여 :). 그래서 delete()에서 이미 삭제할 준비가 된 이드를 가지고 있으며 여기 커서로 엉망이 될 필요가 없습니다. 이것은 내가 원하는대로 작동합니다. 나는 이것을 더 일찍 생각해 봤으면 좋겠다. 그러면 나는 여기서 질문을 게시 할 필요가 없었을 것이다. 이 사람이 도움이되기를 바랍니다!

0

deleteStuff()에 새 Cursor를 작성하고 newView에서 입력 할 때마다 삭제할 ​​항목을 검색합니다. (이 작동합니다)

또한 bindView에서 커서를 사용해 볼 수도 있지만이 작동하는지 확실하지 않습니다.

+0

나는 이것이 좋은 아이디어라고 생각합니다. 'Loader/LoaderManager'와 함께'CursorAdapter'를 사용하는 것은 커서 자체를 관리 할 필요가 없다는 것입니다. – rgamber

+0

로드하기위한 것이고, deleteStuff의 끝에서 cursor.requery()를 시도 할 수도 있습니다),이 메소드는 더 이상 사용되지 않는 것으로 보입니다. 당신이 그것을 시도하고 그것이 작동하는 경우 알려주십시오. 그 동안 나는 더 좋은 방법을 생각할 것입니다. –

+0

또한 bindView (뷰 뷰, 컨텍스트 컨텍스트, 커서 커서)에서 커서를 사용해 볼 수도 있습니다. bindView는 새로 고친 커서를 사용한다고 생각한다. –