0

I'm trying to create a spinner that shows data from an API that returns Pasta objects.

I want the Spinner's selectable label to be Pasta.label, but I want the actual value returned from the Spinner to be another value (Pasta.Id). Kind of like this in HTML:

<select>
    <option value="spag">Spaghetti</option>
    <option value="ravi">Ravioli</option>
</select>

I've seen other questions on SO but none of them seem truly conclusive.

I already have the HashMap<String, String> with the values I need. I just need to figure out how to translate that into a spinner. I thought about just creating a Class like this:

public class SpinnerItem {
    public String key;
    public String value;
}

and populating my Spinner with SpinnerItems, so that when I retrieve the SpinnerItem later on I can just do getSelectedItem().getKey(), but how would I go about writing an ArrayAdapter that does that?

Grumbunks
  • 1,027
  • 1
  • 7
  • 21

1 Answers1

1

You just need toString function in your SpinnerItem class

public class SpinnerItem {
    public String key;
    public String value;

    @Override
    public String toString() {
        return value;
    }
}

There is a readily available sample here as well, hope it helps.

pragnesh
  • 1,240
  • 7
  • 18