How to return a value from a inner class?
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.
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();
}
});
You either need to set your return on an instance variable (not within a method) - but this may lead to concurrency issues, or use a "container" object. Pass-in, or use a "final" method variable, on which you can set the return value you want to return. However, I use the term "return" loosely, as at least in your example, this code won't immediately execute, so you really need to do the processing you're interested within the inner class instead.