I have a array list like this:
private ArrayList<Locations> Artist_Result = new ArrayList<Location>();
This Location class has two properties: id and location.
I need to bind my ArrayList to a spinner. I have tried it this way:
Spinner s = (Spinner) findViewById(R.id.SpinnerSpcial);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, Artist_Result);
s.setAdapter(adapter);
However, it shows the object’s hexadecimal value. So I think I have to set display the text and value for that spinner controller.
The
ArrayAdaptertries to display yourLocation-objects as strings (which causes the Hex-values), by calling theObject.toString()-method. It’s default implementation returns:To make the
ArrayAdadptershow something actually useful in the item list, you can override thetoString()-method to return something meaningful:Another way to do this is, to extend BaseAdapter and implement SpinnerAdapter to create your own Adapter, which knows that the elements in your
ArrayListare objects and how to use the properties of those objects.[Revised] Implementation Example
I was playing around a bit and I managed to get something to work:
I fully commented the code, if you have any questions, don’t hesitate to ask them.