kevin
kevin

Reputation: 827

How to return a value from a inner class?

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.

Upvotes: 7

Views: 21766

Answers (5)

Saurabh Bhurle
Saurabh Bhurle

Reputation: 169

You can do follow simple step:

 create a POJO class object inside method.
 call setter method by setting return value and use it.

Example:

    public void process(){
    Contact contact=new Contact();
    String selectCount="select * from firmId  where id=? for update";
   PreparedStatementCreatorFactory pscf = new 
   PreparedStatementCreatorFactory(selectCount,new int[] {Types.INTEGER});
        pscf.setUpdatableResults(true);
        pscf.setResultSetType(ResultSet.CONCUR_UPDATABLE);
        RowCallbackHandler rch = new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet resultSet) throws SQLException {

             // Here You can set your value
                contact.setContactId(incrementCount);
                resultSet.updateLong("firmCount",incrementCount);
                resultSet.updateRow();
                return;                                                                        
            }
          };
         return contact.getId();
       }

Upvotes: 0

rogerdpack
rogerdpack

Reputation: 66741

The onClick paradigm doesn't let you return values, it lets you respond to "events" later, so you'll have to rethink your code paradigm a bit.

For followers, in the event that the inner class is "blocking" (i.e. not this case), you can return values using AtomicReference, ex:

AtomicReference<String> returnValue = new AtomicReference<>();
someMethod( new Runnable() { returnValue.set("my inner class value");} );
return returnValue.get();

though better would be (if possible) have someMethod modified so it can return your value out itself (and use something besides Runnable in this instance). GL!

Upvotes: 0

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

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.

Upvotes: 11

triggs
triggs

Reputation: 5900

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();
        }
    });

Upvotes: 4

ziesemer
ziesemer

Reputation: 28687

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.

Upvotes: 2

Related Questions