有没有办法改变AlertDialog.Builder里面的价值?(Android)

时间:2017-07-05 09:27:45

标签: android android-alertdialog inner-classes final

我是android新手。 我可以更改AlertDialog.Builder中的值吗?

int changeThisDataToOne = 0;

    new AlertDialog.Builder(view.getContext())
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Are you sure?")
            .setMessage("Do you want to delete this note?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            changeThisDataToOne = 1;
                           //error : Variable 'changeThisDataToOne' is accessed 
                           //from within inner class, needs to be declared final
                        }
                    }
            )
            .setNegativeButton("No", null)
            .show();
}

2 个答案:

答案 0 :(得分:0)

changeThisDataToOne更改为final int[]

final int[] changeThisDataToOne = {0};

然后它的值可以改为

changeThisDataToOne[0] = 1;

或将changeThisDataToOne作为字段变量。

答案 1 :(得分:0)

您无法将本地变量( changeThisDataToOne )访问到内部类中,您应该将其设置为final或将其初始化为全局变量。