How to get Spinner selected item value to string?
Asked Answered
R

14

99

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?

Rendon answered 26/4, 2012 at 10:46 Comment(4)
I am sure, you want selected item from a spinner whenever user clicks on a buttonFrancis
Set the setOnItemSelectedListener outside onClick of your button and inside Onclick method use this interested.getSelectedItem().toString();Division
thanks, my mistake was put the listener inside button listener that why no respondRendon
welcome :) explore api demos ... u can get better examples thereDivision
F
258

Try this:

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

Like this you can get value for different Spinners.

Fortitude answered 26/4, 2012 at 10:58 Comment(0)
C
34
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) {
    }
});
Collectivism answered 5/3, 2014 at 17:59 Comment(1)
But how to get item 0?!Feigin
B
14

You can get the selected item from Spinner by using,

interested.getSelectedItem().toString();
Bushnell answered 26/4, 2012 at 10:49 Comment(0)
D
10

Get the selected item with Kotlin:

spinner.selectedItem.toString()
Deeannadeeanne answered 4/9, 2019 at 17:33 Comment(0)
K
8

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
        }
    });
Kitchenware answered 26/4, 2012 at 11:14 Comment(0)
J
7

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.

Jesher answered 28/5, 2015 at 13:53 Comment(1)
Works great. How do you use "value" outside the onItemSelected member?Rawhide
P
6

In addition to the suggested,

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

You can do,

String Text = String.valueOf(mySpinner.getSelectedItem());
Parboil answered 16/7, 2016 at 5:9 Comment(0)
A
5

Since the latest language for Android Development is Kotlin. Here is, how we do it in Kotlin using Anonymous object.

spinnerName?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
    override fun onNothingSelected(parent: AdapterView<*>?) {
       println("Nothing Selected")
    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
       val selectedString = yourList[position]
    }

}
Afroamerican answered 3/6, 2018 at 18:18 Comment(0)
M
4

I think you want the selected item of the spinner when button is clicked..

Try getSelectedItem():

spinner.getSelectedItem()
Marguerite answered 26/4, 2012 at 10:57 Comment(0)
I
3

When you choose any value from spinner, then you get selected value,

interested.getSelectedItem().toString();
Incognizant answered 11/12, 2014 at 7:42 Comment(0)
A
2

The best way to do this is :-

String selectedItem = spinner.getSelectedItem().toString();

you can refer the docs here : Spinners

Aubine answered 8/2, 2018 at 6:0 Comment(0)
C
2

try this

sp1 = String.valueOf(spinner.getSelectedItem());
Cookstove answered 8/2, 2018 at 19:0 Comment(0)
F
1

By implementing the SpinnerAdapter for your adapter object i use interested.getItem(i).toString()

Ful answered 26/4, 2012 at 10:53 Comment(0)
C
0
    spinnerType = (AppCompatSpinner) findViewById(R.id.account_type);
    spinnerType.setPrompt("Select Type");

    spinnerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            TypeItem clickedItem = (TypeItem) parent.getItemAtPosition(position);
            String TypeName = clickedItem.getTypeName();
            Toast.makeText(AddAccount.this, TypeName + " selected", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
Counterespionage answered 24/3, 2020 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.