Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a value from a inner class?

Tags:

java

android

My code is here:

public static boolean showConfirmationDialog(Context context, String title, String dialogContent) {
        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 ?
            }
        });

right now, I want to return true after I clicked the "confirm" button. so how do I return "true" from a inner class - OnClickListener for the method.

Need some help, thanks.

like image 780
kevin Avatar asked Nov 29 '11 00:11

kevin


People also ask

How do you find the value of an inner class?

Write an inner class in it, return the private members from a method within the inner class, say, getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class.

Can we inherit inner class?

Inner classesA inner class declared in the same outer class (or in its descendant) can inherit another inner class.

Can a private variable be accessed from a inner class?

Inner classes can access the variables of the outer class, including the private instance variables. Unlike the non-static nested classes, the static nested class cannot directly access the instance variables or methods of the outer class. They can access them by referring to an object of a class.

How do I access static nested class?

And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference. They are accessed using the enclosing class name. To instantiate an inner class, you must first instantiate the outer class.


2 Answers

You can't return things from an inner class in this instance. In this case it doesn't make much sense. Is the program supposed to wait inside your onClick function until it returns something? That's not really how listeners work. What you need to do is take what ever code you plan on executing if "true" was returned, and put it inside your inner class.

like image 103
Kurtis Nusbaum Avatar answered Nov 14 '22 22:11

Kurtis Nusbaum


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();
        }
    });
like image 30
triggs Avatar answered Nov 14 '22 22:11

triggs