ChatGPT解决这个技术问题 Extra ChatGPT

How to get Spinner value?

In Android, I am trying to get the selected Spinner value with a listener.

What is the best way to get the spinner's value?


U
Unmitigated
Spinner mySpinner = (Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();

"Value" is pretty ambiguous here. This retrieves the current spinner item title (as in, the String that is displayed to the user), but not its value if you mapped an int array onto the spinner for example.
@Doge to get value of selected item like integer value for example we should make int[] valuesArray and file it in onCreate function maybe from arrays.xml so we can use valuesArray[(int)mySpinner.getSelectedItemId()] to get integer value
E
Erich Douglass

The Spinner should fire an "OnItemSelected" event when something is selected:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        Object item = parent.getItemAtPosition(pos);
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

Thanks -- that worked, though I needed to add the void return type to the methods.
Is there really no way to achieve this without relying on the item's position?
Not a very useful solution if you want to read the spinner value without relying on a selection event.
@Patrick The annotation Override is never required it just tells the compiler "Can you check if this override is done correctly?"
What about getting the default selected item when no change performed on it? @dodo's one is the most proper.
l
lenooh

Say this is your xml with spinner entries (ie. titles) and values:

<resources>
    <string-array name="size_entries">
        <item>Small</item>
        <item>Medium</item>
        <item>Large</item>
    </string-array>

    <string-array name="size_values">
        <item>12</item>
        <item>16</item>
        <item>20</item>
    </string-array>
</resources>

and this is your spinner:

<Spinner
    android:id="@+id/size_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:entries="@array/size_entries" />

Then in your code to get the entries:

Spinner spinner = (Spinner) findViewById(R.id.size_spinner);
String size = spinner.getSelectedItem().toString(); // Small, Medium, Large

and to get the values:

int spinner_pos = spinner.getSelectedItemPosition();
String[] size_values = getResources().getStringArray(R.array.size_values);
int size = Integer.valueOf(size_values[spinner_pos]); // 12, 16, 20

Thx, this should be in the official ANDR doc on spinner, they only showed getting value via Listener.
this exactly what i looking FOR
Perfect solution! android:entries made my day.
C
CommonsWare

Yes, you can register a listener via setOnItemSelectedListener(), as is demonstrated here.


Thanks -- this worked too, and (I think) it makes the code easier to read.
E
Eric Leschinski
View view =(View) getActivity().findViewById(controlId);
Spinner spinner = (Spinner)view.findViewById(R.id.spinner1);
String valToSet = spinner.getSelectedItem().toString();

M
Matt Logan

If you already know the item is a String, I prefer:

String itemText = (String) mySpinner.getSelectedItem();

Calling toString() on an Object that you know is a String seems like a more roundabout path than just casting the Object to String.


w
wafaa hilmy

add setOnItemSelectedListener to spinner reference and get the data like that`

 mSizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
            selectedSize=adapterView.getItemAtPosition(position).toString();