How to get Spinner value?


Question

In Android, I am trying to get the selected Spinner value with a listener.

What is the best way to get the spinner's value?

1
242
11/2/2014 10:22:22 PM

Accepted Answer

Spinner mySpinner = (Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
538
8/21/2018 1:34:46 AM

The Spinner should fire an "OnItemSelected" event when something is selected:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        Object item = parent.getItemAtPosition(pos);
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon