views:

82

answers:

2

When I use ListView to display a array of my object, I can use the following code:

MyObject[] myObject;
...  
ArrayAdapter<MyObject> itemList = new ArrayAdapter<MyObject>(this, R.layout.list, myObject);   
setListAdapter(itemList);

In case that the input is a list:

List<MyObject> myobject;

How can I assign it to ListAdapter? Please advise, Thx!

+1  A: 

There is another constructor for ArrayAdapter that takes in a list:

ArrayAdapter(Context context, int textViewResourceId, List<T> objects)

See ArrayAdapter java doc for a complete list!

Mayra
But I can't use the code:setListAdapter(itemList);Because setListAdapter(ArrayAdapter<MyObject>) is undefined!
eriX
You are referring to the function in ListActivity: public void setListAdapter(ListAdapter adapter)? ArrayAdapter<T> extends from ListAdapter, so you can provide an ArrayAdapter<T> instance to that function... Try it!
Mayra
Yes! It's working now. Thx :)
eriX
A: 

Even the question seems already to be answered, i'd like to mention using custom listviews with a custom adapter:

There is a great tutorial how to use a listview

http://developerlife.com/tutorials/?p=327

a second tutorial (in 6 parts) you can find here:

http://www.androidguys.com/2008/07/14/fancy-listviews-part-one/

Roflcoptr