AlertDialog无法正常工作

时间:2017-12-25 06:13:24

标签: java android onclick alertdialog

我正在创建一个AlertDialog,它会询问用户是否要删除记录?所以我已经声明了一个全局标志变量(在o​​nCreate()方法之上)

  

private int yes;

如果用户按,则“是”的值将为1&  如果按,则“是”的值将为0

我的AlertDialog代码位于

之下
public int dialog()
        {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(DataListActivity.this);
            alertDialog.setTitle("Alert");
            alertDialog.setMessage("Are you sure to delete ?");
            alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                yes = 1;
                }
            });
            alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    yes=0;
                }
            });
            alertDialog.show();
            return yes;
        }

基于这个是我想要删除记录但是我按是或否,这个标志的值int yes仍为0,参见LOGCAT

这个用于按否

12-25 00:52:22.144 2133-2133/? E/Logggggggg::  0

这个按下是

12-25 00:52:33.408 2133-2133/? E/Logggggggg::  0

现在我正在检查标志是,

     int dd = dialog();
     Log.e("Logggggggg: "," "+yes);
     if (dd == 1)
     {
        Boolean r = mydb.deleteData(selections);
     }
     else
     {
      /////// do Nothing;
     }

谁能告诉我这里出了什么问题.. ??

3 个答案:

答案 0 :(得分:2)

您无法捕获yes的值作为方法的返回值,因为在return语句发生时尚未设置它。相反,只需在onClick侦听器中直接对yes和no按钮进行数据库清理,例如。

alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // delete the record here
        Boolean r = mydb.deleteData(selections);
    }
});

以上内容应视为伪代码,因为我不熟悉代码库的详细信息。但是通过直接在onClick监听器中处理该操作来响应用户选择“是”的基本思路。

答案 1 :(得分:1)

试试这个

private void dialog() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
    alertDialog.setTitle("Alert");
    alertDialog.setMessage("Are you sure to delete ?");
    alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            yes = 1;
            Toast.makeText(getActivity(), String.valueOf(yes), Toast.LENGTH_SHORT).show();
        }
    });
    alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            yes =0;
            Toast.makeText(getActivity(), String.valueOf(yes), Toast.LENGTH_SHORT).show();

        }
    });
    alertDialog.show();
}

检查标志

if(yes==1){

    Boolean r = mydb.deleteData(selections);
 }else
 {
  /////// do Nothing;
 }

答案 2 :(得分:0)

你可能只需要使用onClick()签名值,即int which并为其分配yes值,如下面的代码

  public int dialog()
    {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(LoginActivity.this);
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Are you sure to delete ?");
        alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                which = 1;
                yes = which;
                Toast.makeText(LoginActivity.this,"value : "+yes,Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                which = 0;
                yes = which;
                Toast.makeText(LoginActivity.this,"value : "+yes,Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog.show();

        Toast.makeText(LoginActivity.this,"value : "+yes,Toast.LENGTH_SHORT).show();
        return yes;

    }