no-name
no-name

Reputation: 477

How to access variable from spinner listener?

I have the following code, in which I want to access 'selectedTeam' at the button listener.

        //Adding setOnItemSelectedListener method on spinner.
        sTeams.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                selectedTeam = parent.getItemAtPosition(position).toString();
                editText.setText(selectedTeam, TextView.BufferType.EDITABLE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        buttonApply.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                String editedName = editText.getText().toString();
                // Here I want to access selectedTeam
            }
        });

I tried to declare the variable outside the method but that gives the error 'Variable 'selectedTeam'is accessed from within inner class, needs to be declared final'. I tried that, but that doesn't work since final Strings cannot be changed.

Upvotes: 1

Views: 810

Answers (3)

C&#224; ph&#234; đen
C&#224; ph&#234; đen

Reputation: 1951

Use class member instead.

In JLS 8.1.3. Inner Classes and Enclosing Instances:

When an inner class (whose declaration does not occur in a static context) refers to an instance variable that is a member of a lexically enclosing class, the variable of the corresponding lexically enclosing instance is used.

Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final.

It means you can only use an outside final variable or an enclosing class member in an anonymous inner class.

[...]
private String selectedTeam;
[...]

//Adding setOnItemSelectedListener method on spinner.
    sTeams.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
            selectedTeam = parent.getItemAtPosition(position).toString();
            editText.setText(selectedTeam, TextView.BufferType.EDITABLE);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    buttonApply.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            String editedName = editText.getText().toString();
            if (selectedTeam != null && !"".equals(selectedTeam)) {
                // Do something
            }
        }
    });

Upvotes: 2

user8259253
user8259253

Reputation:

In my case I use shared preference to store the value and get that value where i want to use.

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "selectedTeam");
editor.apply();

How to get value:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("name", null);

Hope it helps

Upvotes: 0

Nawako
Nawako

Reputation: 341

You need to declare your variable global.

public class MainActivity extends AppCompatActivity {

String selectedteam;
...
buttonApply.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            String editedName = editText.getText().toString();
            selectedteam = editedName; // or whatever you want
        }
    });

Upvotes: 1

Related Questions