AlertDialog无法正常工作

问题描述 投票:0回答:3

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

private int yes;

如果用户按是,则yes的值为1,如果按否,则yes的值为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;
     }

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

java android onclick alertdialog
3个回答
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
投票

试试这个

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;
 }

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;

    }
© www.soinside.com 2019 - 2024. All rights reserved.