0

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!

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

But I can't use the code: setListAdapter(itemList); Because setListAdapter(ArrayAdapter<MyObject>) is undefined!
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!
0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.