我是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();
}
答案 0 :(得分:0)
将changeThisDataToOne
更改为final int[]
,
final int[] changeThisDataToOne = {0};
然后它的值可以改为
changeThisDataToOne[0] = 1;
或将changeThisDataToOne
作为字段变量。
答案 1 :(得分:0)
您无法将本地变量( changeThisDataToOne )访问到内部类中,您应该将其设置为final或将其初始化为全局变量。