get value of string outside void async method?

Viewed 68

How do I get the value of String r outside the getInformation method? Specifically, I want to set String stringpwd equal to String r. (getInformation is a void method)

EDIT: I forgot to mention, I only want to set the value stringpwd after success has been completed.

                String user = emailaddress.getText().toString().trim();
                String pwd = password.getText().toString().trim();
                db.getInformation("password", user, new DatabaseHelper.MyCallback() {
                    @Override
                    public void success(String r) {
                        // Use result

                    }

                    @Override
                    public void failure(Throwable t) {
                        // Display error

                    }

                });

            

               String stringpwd = r;
1 Answers

You can define a global variable (example used below: userCpy), and initialise or store the value of string r in that variable making it availabe, elsewhere.

                String userCpy = null;         
                int flag = 0;    
                String user = emailaddress.getText().toString().trim();
                String pwd = password.getText().toString().trim();
                db.getInformation("password", user, new DatabaseHelper.MyCallback() {
                    @Override
                    public void success(String r) {
                        // Use result
                       userCpy = r;
                       flag = 1;
                    }

                    @Override
                    public void failure(Throwable t) {
                        // Display error

                    }

                });

               if(flag==1)
                   String stringpwd = userCpy;
Related