Alex Rivera | Logout

Android Spinner databind using array list

Asked 2011-07-03T10:07:01.217
14

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.

Edit
Report

1 Answer

2

Thank Lukas, you help me a lot. I d'like to improve your answer. If you just want to access the selected item later, you can use this :

Spinner spn   = (Spinner) this.findViewById(R.id.spinner);
Guy     oGuy  = (Guy)     spn.getSelectedItem();

So you don't have to use the setOnItemSelectedListener() in your initialisation :)

answered 2012-07-11T13:06:43.587

Your Answer