How to return a value from a inner class?

前端 未结 5 577
抹茶落季
抹茶落季 2021-01-12 18:39

My code is here:

public static boolean showConfirmationDialog(Context context, String title, String dialogContent) {
        AlertDialog.Builder builder = ne         


        
5条回答
  •  青春惊慌失措
    2021-01-12 18:59

    OnClickListeners dont return values. Without knowing what exactly you need to do when the click listener fires I cant give you any specifics but

    private boolean classBoolean = false;
    public static boolean showConfirmationDialog(Context context, String title, String    dialogContent) {
    
        //local variables must be declared final to access in an inner anonymous class
        final boolean localBoolean = false;
    
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle(title);
        builder.setMessage(dialogContent);
        builder.setPositiveButton("Confirm", new OnClickListener() {
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // what to do ?
                //you can't change a local var since to access it it needs to be final
                //localBoolean = true; can't do this
                //so you can change a class var
                classBoolean = true;
                //or you can also call some method to do something
                someMethod();
            }
        });
    

提交回复
热议问题