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?


Solution 1:

Try this:

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

Like this you can get value for different Spinners.

Solution 2:

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) {
    }
});

Solution 3:

You can get the selected item from Spinner by using,

interested.getSelectedItem().toString();

Solution 4:

try this

 final Spinner cardStatusSpinner1 = (Spinner) findViewById(R.id.text_interested);
    String cardStatusString;
    cardStatusSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

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

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

        }
    });

 final Button saveBtn = (Button) findViewById(R.id.save_button);
    saveBtn .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            System.out.println("Selected cardStatusString : " + cardStatusString ); //this will print the result
        }
    });

Solution 5:

If your Spinner was populated by SQLite cursor, then the solution is:

Spinner mySpin = (Spinner) findViewById(R.id.myspin);
mySpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
           SQLiteCursor item = (SQLiteCursor) parent.getItemAtPosition(position);
           String value = String.valueOf(item.getString(0));
           Toast.makeText(getApplicationContext(), "The option is:" + value , Toast.LENGTH_SHORT).show(); 
 }

PS: In item.getString(0) -> 0 is the index of column on cursor that you want to get.