@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String choice="";
AlertDialog.Builder builder1 = new AlertDialog.Builder(parent.getContext());
builder1.setMessage("Do you want to accept this vendor's quote?");
builder1.setCancelable(true);
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
choice= "true" //cant access non final variable
}
});
builder1.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
if(choice.equals("true"){
// do something
}
}
如何在不使其成为最终版本的情况下访问“选择”变量,或者有任何其他方式来了解用户是否按下了正按钮或负按钮
答案 0 :(得分:3)
将您的变量声明为类中的字段:
public class YourClass {
private String choice;
...
}
答案 1 :(得分:2)
干净的方法是让onClick
方法调用&#34; setter&#34;你父类的方法。
public class Activity {
private boolean choice = false;
...
//In your method
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
setChoice(true);
}
});
}
...
public void setChoice(boolean choice) {
this.choice = choice;
}
}
答案 2 :(得分:0)
变量必须是最终的,例如Eclipse说:
不能引用在不同方法中定义的内部类中的非最终变量选择
答案 3 :(得分:0)
将变量声明到外部,就在类定义中。