I have an Android Spinner widget in which I’d like to display a slightly different string for the first item if it’s selected vs. if it’s being displayed in the drop-down. For example, clicking
[Select a dessert... v]
Pops up:
o No dessert
Cake
Pie
Ice cream
If e.g. “Cake” is chosen, the Spinner behaves normally:
[Cake v]
But if “No dessert” is chosen, Select a dessert... should be used as the Spinner prompt, as when nothing was selected.
What’s the easiest way to do this?
Assuming you’re using an approach similar to the one below, the
simple_spinner_dropdown_itemdefaults to thetoString()value of the object in the adapter. With that in mind, override thetoString()method of some customListItemclass to return whatever value you want the spinner to display when the item is being shown as a dropdown item. Meanwhile override the getView for the item selected in the adaptor.