How do you get the selected spinner value?

I am trying to get a row of selected items from Spinner . So far, I got this:

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

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

+46
java android spinner
Apr 16 '10 at 11:13
source share
8 answers

To get the selected counter value, you can follow this.

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 Spinner.

Inside the onItemSelected method of this class, you can get the highlighted element:

 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 must be registered with Spinner:

 spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); 
+72
Apr 16 2018-10-16T00:
source share

You have getSelectedXXX methods from the AdapterView class from which Spinner is called:

getSelectedItem ()

getSelectedItemPosition ()

getSelectedItemId ()

+52
Apr 16 '10 at 11:52
source share

Just use this:

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

This will give you the String selected item in Spinner .

+41
Feb 13 '12 at 11:12
source share

mySpinner.getItemAtPosition(mySpinner.getSelectedItemPosition()) works based on a rich description.

+16
Aug 14 '11 at 19:33
source share

Depends on what point you want to "catch" the value.

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

If you are more likely to catch the value when the user completes the final task, for example, by clicking the "Submit" button, or something like that, the answer is better provided by Rich.

+5
Nov 17 '10 at 11:51
source share

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 } }); 
+1
Aug 19 '15 at 7:03
source share

To get only the string value inside the counter:

spinner.getSelectedItem () ToString () ;.

+1
Sep 29 '15 at 15:47
source share
 Spinner spinner=(Spinner) findViewById(R.id.spinnername); String valueinString = spinner.getSelectedItem().toString(); 

In case of value of Spinner int it outputs to int

 int valueinInt=(int)(spinner.getSelectedItem()); 
0
Jun 19 '17 at 17:04 on
source share



All Articles