Skip to content
Advertisement

How do you get the selected value of a Spinner?

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

JavaScript

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?

Advertisement

Answer

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:

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement