我目前面臨着一個愚蠢的問題。我有一個自定義適配器的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);
(...)
}
(...)
}
我在我的適配器上實現了一個接口,以在項目被刪除時通知列表,然後如您所建議的那樣,我重新啓動我的加載程序。它現在有效;) – colletjb 2013-03-06 07:53:30