【发布时间】:2011-01-11 10:56:37
【问题描述】:
我正在尝试使用删除功能实现 ListView 以从列表视图中删除项目。从数据库中删除项目后,我成功删除但未能刷新列表视图。
实际上,单击 listitem,我正在为“删除”和“取消”操作显示 AlertBox,单击“删除”时,应从数据库中删除项目,以及从列表视图和列表视图中删除。我也用过notifyDataSetChanged()方法。
lview = (ListView) findViewById(R.id.lview);
adapter = new ListView_CustomAdapter(this, listitemDisplay);
lview.setAdapter(adapter);
lview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Show_Alert_box(v.getContext(),"Please select action.",position);
}
});
以及 Show_Alert_box 的代码:
public void Show_Alert_box(Context context, String message,int position)
{
final int pos = position;
final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(getString(R.string.app_name_for_alert_Dialog));
alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
try
{
db.open();
String[] whereArgs={String.valueOf(pkID)};
return db.delete(DATABASE_TABLE_4,"pk_pkID == ?",whereArgs);
adapter.notifyDataSetChanged();
db.close();
}
catch(Exception e)
{
}
} });
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
} });
alertDialog.setMessage(message);
alertDialog.show();
}
【问题讨论】:
-
您使用的是哪种适配器?如果您使用 CursorAdapter,您所描述的应该可以工作。
-
Mods 不会删除问题,因为您对答案不满意,抱歉。