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

    }


我如何在不使其最终化的情况下访问“选择”变量,或者是否有其他方法可以知道用户是否按下了肯定按钮或否定按钮

最佳答案

在类中将变量声明为字段:

public class YourClass {
    private String choice;
    ...
}

07-24 19:58