Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Spinner selected item value to string?

I have 5 Spinners. In order to make it summary to this.

This is Spinner in xml

<Spinner             android:id="@+id/text_interested"             android:layout_span="2"             android:layout_width="wrap_content"             android:layout_height="60px"             android:entries="@array/interestedarrays"             android:prompt="@string/interestedprompt" /> 

This is Spinner in Java

submitbtn.setOnClickListener(new OnClickListener() {         public void onClick(View v) { interested.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {                         public void onItemSelected(                                 AdapterView<?> adapterView, View view,                                 int i, long l) {                             interesting = interested.getItemAtPosition(i).toString();                         }                          public void onNothingSelected(                                 AdapterView<?> adapterView) {                          }                     });     } }); 

Explanation here:

The page got a button. This button will read the data from spinner when pressed. I checked the output with this

System.out.println(interested.getItemAtPosition(i).toString()); 

It gave me nothing not even null.

How to retrieve the value and to string it?

like image 581
Alan Lai Avatar asked Apr 26 '12 10:04

Alan Lai


2 Answers

Try this:

String text = mySpinner.getSelectedItem().toString(); 

Like this you can get value for different Spinners.

like image 168
Bhavin Avatar answered Oct 13 '22 02:10

Bhavin


String Text = mySpinner.getSelectedItem().toString(); 

OR

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {         Object item = parent.getItemAtPosition(position);     }     public void onNothingSelected(AdapterView<?> parent) {     } }); 
like image 40
santoshpatmca Avatar answered Oct 13 '22 02:10

santoshpatmca