如何管理删除CursorAdapter上的项目

时间:2013-03-05 15:09:44

标签: android android-cursoradapter

我目前面临着一个愚蠢的问题。我有一个带有自定义适配器的listview(扩展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);
        (...)
    }
    (...)
}

4 个答案:

答案 0 :(得分: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 :(得分:1)

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

如果您想要真正的表现,请使用AsyncTaskLoader

答案 2 :(得分: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数据已更改,并且必须再次构建适配器。

如果重新查询需要很长时间,这将是一个激烈的操作。考虑在另一个线程中进行。

答案 3 :(得分:1)

删除数据库中的项目后,请致电notifyDataSetChanged上的CommentCursorAdapter,并使用您正在使用的匿名内部课程授予您final this CursorAdapter因为这是在ListView内部。这应该会触发您{{1}}的刷新。

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