0

I have check out answers like thees Variable is accessed within inner class. Needs to be declared final but it does not address what I am after

I have an on click listener and here is the code

mUsername is a EditText that is already defined same with the button

mLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String username = mUsername.getText().toString();
            username = username.trim();
            ParseUser.logInInBackground(username, password, new LogInCallback() {
                    @Override
                    public void done(ParseUser parseUser, ParseException e) {
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            intent.putExtra("username", username);// get compile error here 
                            startActivity(intent);
                   }
          }
});

The error says to set String username = mUsername.getText().toString(); to final but then I can't redefine username with the trim? I don't know why it can be used in the logInInBackround as a parameter but not in the method?

Thanks for the help in advance.

Community
  • 1
  • 1
iqueqiorio
  • 1,149
  • 2
  • 35
  • 78

1 Answers1

1

You could trim it directly:

String username = mUsername.getText().toString().trim();

This saves you the intermediate local variable store.

nanofarad
  • 40,330
  • 4
  • 86
  • 117