在Android中使用AlertDialog确认删除

在Android中使用AlertDialog确认删除,android,database,android-alertdialog,confirm,Android,Database,Android Alertdialog,Confirm,所以我试图让AlertDialog确认点击“从数据库删除”按钮。已经尝试了相当长的一段时间,无法得到一个工作模型。我目前的代码如下 在类布尔的顶部 private static boolean dialogResult; 显示对话框的方法 private void showErrorDialog(String title, String message){ AlertDialog.Builder builder = new AlertDialog.Builder(this) .

所以我试图让AlertDialog确认点击“从数据库删除”按钮。已经尝试了相当长的一段时间,无法得到一个工作模型。我目前的代码如下

在类布尔的顶部

private static boolean dialogResult;
显示对话框的方法

private void showErrorDialog(String title, String message){
    AlertDialog.Builder builder = new AlertDialog.Builder(this)
    .setTitle(title)
        .setMessage(message)
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Activity.setDialogResult(true);
                dialog.dismiss();
                }
            })
        .setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Activity.setDialogResult(false);
                dialog.dismiss();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}
其中setDialogResult(bool)只设置前面提到的类变量

按下“删除”按钮时,此代码执行:

setDialogResult(false);
//Find which list is to be deleted
        RadioButton selectedBtn=(RadioButton)findViewById(group.getCheckedRadioButtonId());
        String currentSelectedListName=selectedBtn.getText().toString();

            if(!currentSelectedListName.equals("All")){//check if the list is the default
                showErrorDialog("","Delete: "+currentSelectedListName+" ?");
                if(dialogResult){
                    mDbHelper.deleteFGList(currentSelectedListName);
                    this.populateRadioGroupFromDb();
                }
            }  else{//if it is the default (That is, the name of the list is "All") tell user of error
                showErrorDialog("Delete Error","The default list cannot be deleted");
            }
        //}
谢谢你的帮助

编辑:对不起,忘了提到我的错误。在“警报”对话框中单击“确定”时,不会从数据库中删除该项。不知怎的,它跳过了布尔检查。您可以看到,我在该代码段的开头将布尔值设置为false。如果已删除,则按对话框中的“删除”按钮和“确定”按钮,然后什么也不发生。如果再次按下“删除”按钮,则此新选择将自动删除,而无需进行对话框检查。基本上,那些onClick方法是一团糟的,我希望有更好的方法。我读过一些关于使用接口的书

所以人们似乎一直在建议这种方法

if(!currentSelectedListName.equals("All")){//check if the list is the default
                AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("")
                    .setMessage("Delete: "+currentSelectedListName+" ?")
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                                mDbHelper.deleteFGList(currentSelectedListName);
                                Activity.populateRadioGroupFromDb();
                            }
                    })
                    .setNegativeButton(R.string.cancel,null);
                AlertDialog alert = builder.create();
                alert.show();
            }  else{//if it is the default (That is, the name of the list is "All") tell user of error
                showErrorDialog("Delete Error","The default list cannot be deleted");
            }
问题是eclipse在上给出了编译错误

mDbHelper.deleteFGList(currentSelectedListName);
Activity.populateRadioGroupFromDb();
1.无法在不同类型中定义的内部类内引用非最终变量currentSelectedListName 方法 2.无法从MyFGCardsActivity类型对非静态方法populateRadioGroupFromDb()进行静态引用


这是提倡的方法还是我误解了?

称这两行为setPositiveButton

mDbHelper.deleteFGList(currentSelectedListName);
this.populateRadioGroupFromDb();

谢谢

Android对话框的设计不是模态的。也就是说,它们不会阻止代码的执行,直到被解除,但是
show()
会立即返回。因此,当您编写这样的代码时:

showErrorDialog("","Delete: "+currentSelectedListName+" ?");
if(dialogResult){
对话框result
尚不可用

现在您提到了一些关于“接口”的内容,这是解决方案的一部分。您应该分配回调,例如分配给执行所需操作的正按钮和负按钮。这些回调是根据Java接口实现的。实际上,您已经有了这样的回调接口:
DialogInterface.OnClickListener

因此,将侦听器中应在正点击时运行的任何代码放在正按钮上,例如删除代码,并将应在负点击时运行的任何代码放在相应的侦听器中,例如nothing。并去掉
dialogResult
变量,以及可能推广这种编码风格的任何教程(使用静态类变量获取本地状态信息)


在这里调用
onClick()
监听器中的
dismise()
是多余的:当您这样分配监听器时,当您的监听器运行时,对话框将已经被取消。

您面临什么问题?对话框显示了吗?很遗憾,setPositiveButton中的OnClick方法被认为是在另一个类中,因此currentSelectedListName未被传递,ActivityClass.populateRadioGroupFromDb();由于静态、非静态问题而无法工作。尝试过这个方法,我同意dialogResult方法是一个糟糕的方法,这是我能找到的最好的方法。我编辑了我原来的帖子,这就是你的意思吗?我没有太多创建可用接口的经验,所以现在我真的很确定我将如何去做