如果用户在警告对话框中按下是按钮,如何在列表中添加项目,如果用户按否,则不添加项目

时间:2011-07-21 15:43:50

标签: java android dialog alertdialog

我正在创建一个应用程序,如果用户选择了一个 子菜单项我弹出一个警告对话框,询问他的确认 他是否希望将该项目保存在他的列表中并保存 如果按下否,则按是,不添加。

2 个答案:

答案 0 :(得分:1)

您可以使用它来显示警告:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure to do this?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    doSomeThing();
                                            dialog.cancel();
                }
            }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();

答案 1 :(得分:0)

您可以将从对话框中获得的元素添加到用于馈送Adapter然后提供cursor = new yourAdapter(YourClass.this.getBaseContext(), android.R.layout.simple_list_item_1, dataList);

的ArrayList中

调用cursor.notifyDataSetChanged();告诉列表重新填充新数据;