How to return a value from a inner class?

前端 未结 5 536
抹茶落季
抹茶落季 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();
            }
        });
    
    0 讨论(0)
  • 2021-01-12 19:04

    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.

    0 讨论(0)
  • 2021-01-12 19:07

    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.

    0 讨论(0)
  • 2021-01-12 19:11

    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!

    0 讨论(0)
  • 2021-01-12 19:18

    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();
           }
    
    0 讨论(0)
提交回复
热议问题