Click here to download the example program.
In order to understand the trick, one has to know, how Adapters work in general and ArrayAdapter in particular. Adapters are objects that are able to turn data structures into widgets. The widgets are then displayed in a List or in a Spinner. So the two questions an Adapter answers are:
- What widget or composite view to associate with a data structure with a certain index.
- How to extract data from the data structure and how to set field(s) of the widget or composite view according to this data.
- Each widget for any index is the same and is inflated from the resource whose ID the ArrayAdapter receives in its constructor.
- Each such a widget is expected to be an instance of TextView (or descendant). The widget's setText() method will be used with the string format of the item in the supporting data structure. The string format will be obtained by invoking toString() on the item.