2013-03-05 30 views
5

我目前面臨着一個愚蠢的問題。我有一個自定義適配器的ListView(擴展CursorAdapter)。如何管理刪除CursorAdapter上的項目

它顯示不同按鈕(共享,像,刪除)的自定義視圖。對於刪除按鈕,它有一個alertdialog來確認刪除該項目。當用戶確認時,該項目從數據庫中刪除。一切工作正常,直到這裏。

我的問題是,我怎樣纔能有效地用我的新數據集更新我的列表視圖?

非常感謝您的幫助。

代碼:

public class CommentCursorAdapter extends CursorAdapter{ 
    (...) 
    @Override 
    public void bindView(View view, Context arg1, Cursor cursor) { 
    (...) 
     holder.list_item_comment_discard_btn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final String _id = v.getTag().toString(); 
      AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); 
      builder.setTitle("Delete"); 
      builder.setMessage("Do you want to delete "+_id); 
      builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // User clicked OK button 
        DBAdapter dba = new DBAdapter(mActivity); 
        dba.open(); 
        dba.remove(_id); 
        Log.i("TAAG", "removed: "+_id); 
        dba.close();  

       // How to update the listview ??          
       } 
      }); 
      builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // User cancelled the dialog 
       } 
      }); 

      AlertDialog d = builder.create(); 
      d.show(); 
     } 
     }); 
     holder.list_item_comment_discard_btn.setTag(_id); 
     (...) 
    } 
    (...) 
} 

回答

9

如果使用LoaderManager來管理您的遊標的生命週期(即做這種正確的方式),你只需要調用restartLoader,然後(重新)設置onLoadFinished適配器的光標。您的列表視圖將重新載入更新的數據。

在你的活動或片段光標的管理應以一種非常簡單和直接的方式來完成:

  • 爲了初始化光標:

    public void onCreate(Bundle savedInstanceState) { // or onStart 
    // ... 
    this.getLoaderManager().initLoader(MY_CURSOR_ID, null, this); 
    // ... 
    } 
    
    public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
    // TODO: create a cursor loader that will load your cursor here 
    } 
    
    public void onLoadFinished(Loader<Cursor> arg0, final Cursor arg1) { 
    // TODO: set your cursor as the cursor of your adapter here; 
    } 
    
    public void onLoaderReset(Loader<Cursor> arg0) { 
    // TODO: set null as the cursor of your adapter and 'free' all cursor 
    // references; 
    } 
    
  • 然後,當您需要重新載入您的數據時:

    this.getLoaderManager().restartLoader(MY_CURSOR_ID, null, this); 
    
不推薦使用
+1

我在我的適配器上實現了一個接口,以在項目被刪除時通知列表,然後如您所建議的那樣,我重新啓動我的加載程序。它現在有效;) – colletjb 2013-03-06 07:53:30

1

呼叫 cursor.requery(); notifyDataSetChanged();

如果你想要真正的表現使用AsyncTaskLoader

+0

! – Online98 2015-04-09 09:56:54

+0

它不起作用! :( – Jorgesys 2017-02-24 08:10:04

1

只需重新查詢Cursor這樣的:

public void onClick(DialogInterface dialog, int id) { 
    // User clicked OK button 
    DBAdapter dba = new DBAdapter(mActivity); 
    dba.open(); 
    dba.remove(_id); 
    Log.i("TAAG", "removed: "+_id); 
    dba.close();  
    cursor.requery(); 
    notifyDataSetChanged();         
} 

,然後調用notifyDataSetChanged()告訴該數據已經改變了Adapter,它必須重新構建適配器。

如果詢問需要很長時間,這將是一個激烈的操作。考慮在另一個線程中進行。

+0

我測試了,但它沒有工作。此外,cursor.requery()已棄用:( – colletjb 2013-03-05 15:22:09

+0

)如果它被棄用,你可以使用像@Loxley中提到的'AsyncTaskLoader' – tolgap 2013-03-05 15:24:16

1

後您刪除的項目在數據庫中,你使用你需要,因爲這是一個final參考thisCursorAdapter裏面您CommentCursorAdapter,這個匿名內部類授予調用notifyDataSetChanged。這應該會觸發您的ListView刷新。

http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()

+0

我試過這個沒有成功:'(:builder.setPositiveButton(android.R.string.ok,new DialogInterface。OnClickListener(){ \t \t \t \t \t公共無效的onClick(DialogInterface對話,詮釋的id){ \t \t \t \t \t \t //用戶單擊OK按鈕 \t \t \t \t \t \t將對DBAdapter DBA =新將對DBAdapter(mActivity); \t \t \t \t \t \t dba.open(); \t \t \t \t \t \t dba.remove(_id); \t \t \t \t \t \t Log.i(「TAAG」,「removed:」+ _id); \t \t \t \t \t \t dba.close(); \t \t \t \t \t \t \t \t \t \t \t \t \t \t cursor.requery(); \t \t \t \t \t \t mInstance.notifyDataSetChanged(); \t \t \t \t \t} \t \t \t \t}); – colletjb 2013-03-05 15:25:04

相關問題