0

My first android project! I have a Login Activity. Within it there are two EditText fields, "username" and "password". I've made a listener for when the user has completed the field, namely an onEditorActionListener. My motivation behind this is that I need the username and password fields as a String Variable to send to Volley and onto an API. The password Listener is nearly identical to the code below.

final EditText loginEditText_User = (EditText) findViewById(R.id.login_user);

loginEditText_User.setOnEditorActionListener(new TextView.OnEditorActionListener() {
   @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_NEXT){
            // TODO Retrieve Username here
            Log.d("[userName]", loginEditText_User.getText().toString());
            loginEditText_Pass.requestFocus();
        }
        return true;
    }
});

I've tried simply declaring a variable String userName before the listener and setting the value within the block via userName = loginEditText_User.getText().toString();, but this doesn't work because "the variable 'userName' is accessed from within inner class, needs to be declared final". However, but when declared final, I obviously "cannot assign value to final variable".

I've seen related questions such as this and this, but they're not quite the same thing.

Thanks in advance!

Community
  • 1
  • 1
Zev Isert
  • 915
  • 11
  • 20

3 Answers3

1

You can just define you userName variable as a field of your activity, and then you will have an access to it in your scope:

private String userName;
romtsn
  • 11,704
  • 2
  • 31
  • 49
  • Thank you! This worked, surprisingly enough to me.. For anyone coming across this problem in the future, it makes less sense to try an capture the user's _username_ and _password_ within the listener as I had asked above, this approach fails if the user taps on the next input field rather than sending `IME_ACTION_NEXT` – Zev Isert Apr 14 '15 at 19:55
0

I just answered someone else's question about the same thing here: Edit text first time to input a letter validation

If you use a TextWatcher interface instead, you can get the updated text every time the user adds or removes a letter. Now, If you don't care about the current text until they hit your login button, then you could just do something like. editText.getText() and that returns the current text. The nice thing about the TextWatcher, is you can enable/disable the login button with valid/invalid text. And, it gets you around that whole 'final' issue you mentioned.

Community
  • 1
  • 1
Jacob Holloway
  • 887
  • 8
  • 24
0

This is a known issue every programmer has to deal with a couple of times, there are some options to work around this problem :

1)Use a set function to change the value of the variable, functions can be called anywhere within an inner class.

2)Assign a new variable, which you initialized inside the inner class, with the value you want and use the new variable

Avrham Aton
  • 138
  • 6