53

I am trying to get the selected items string out of a Spinner. So far I have gotten this:

bundle.putString(ListDbAdapter.DB_PRI, v.getText().toString());

This does not work and gives a class casting exception (I thought I could cast a View to a widget that inherits it. Obviously not!) So how do you get the selected value of a Spinner?

Matthew Hall
  • 533
  • 1
  • 4
  • 4

9 Answers9

74

To get the selected value of a spinner you can follow this example.

Create a nested class that implements AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner.

Within "onItemSelected" method of that class, you can get the selected item:

public class YourItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selected = parent.getItemAtPosition(pos).toString();
    }

    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

Finally, your ItemSelectedListener needs to be registered in the Spinner:

spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
jalopaba
  • 8,039
  • 2
  • 44
  • 57
55

You have getSelectedXXX methods from the AdapterView class from which the Spinner derives:

getSelectedItem()

getSelectedItemPosition()

getSelectedItemId()

Rich
  • 36,270
  • 31
  • 115
  • 154
46

Simply use this:

spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();

This will give you the String of the selected item in the Spinner.

Community
  • 1
  • 1
Tharaka Devinda
  • 1,952
  • 21
  • 23
16

mySpinner.getItemAtPosition(mySpinner.getSelectedItemPosition()) works based on Rich's description.

biegleux
  • 13,179
  • 11
  • 45
  • 52
Chrispix
  • 17,941
  • 20
  • 62
  • 70
5

Depends ay which point you wish to "catch" the value.

For instance, if you want to catch the value as soon as the user changes the spinner selected item, use the listener approach (provided by jalopaba)

If you rather catch the value when a user performs the final task like clicking a Submit button, or something, then the answer provided by Rich is better.

jalopaba
  • 8,039
  • 2
  • 44
  • 57
Tivie
  • 18,864
  • 5
  • 58
  • 77
2

This is another way:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int pos, long arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
Sasa
  • 565
  • 5
  • 15
1

To get just the string value within the spinner use the following:

spinner.getSelectedItem().toString();
Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
dandev91
  • 1,691
  • 3
  • 22
  • 34
0
Spinner spinner=(Spinner) findViewById(R.id.spinnername);
String valueinString = spinner.getSelectedItem().toString();

In Case Spinner values are int the typecast it to int

int valueinInt=(int)(spinner.getSelectedItem());
Adnan Ali
  • 2,851
  • 5
  • 22
  • 39
0

Useful code/syntax to simply get the selected index/value and assign it to a sutiable variable;

int i = ((Spinner)findViewById(R.id.spinnerDistance)).getSelectedItemPosition();
String s = ((Spinner)findViewById(R.id.spinnerDistance)).getSelectedItem().toString();
Clarius
  • 1,183
  • 10
  • 10