views:

97

answers:

3

Hey guys,

I tried to add these views to list view using this kind of factory but everytime I try and add the view to a ListActivity, it comes up with nothing. What am I doing wrong? I set my list views like so:

 List<View> views = new ArrayList<View>();
 for(int x =0;x<tagg_views.size();x++){
      lv.addHeaderView(views.get(x));
 }
+1  A: 

It looks like you are trying to add x number of headers to your ListView. That doesn't make sense.

A ListView should contain x number of copies of the same view, with different information on each line.

Hello ListView gives a good example of the correct usage of a ListView.

Mayra
I understand that, but they are the same TYPE of view, the listview shouldn't know whats inside the view that i give it -- only that it should display the view that i give it. Do you know what I mean?
hwrdprkns
That is true, in the Hello ListView example all of the views are of type list_item. You don't say what is in your views, but you should be able to take that example as a starting place, and replace list_item with your particular view. Did you try that?
Mayra
I did and I was able to get it working. Thank you very much for your help!
hwrdprkns
+1  A: 

Why are you adding the Views to the list yourself? I would highly recommend using any kind of apropriate Adapter for the List. The adapter will handle the creating and recycling of views while the user is scrolling etc. If you use an Adapter it is discouraged to save references to the view yourself like you are doing it in the views list.

The addHeaderView method you are using is made to one single header to the list that always will appear on the top of the list. This means calling it in a loop will not have a reasonable result.

Look into the helloListView example Mayra mentions to understand how a list in android is working. To see how a custom listadapter works have a look at this tutorial looks promising despite the bad code formatting.

Janusz
+1  A: 

A ListView is linked with and Adapter. The Adapter is responsible for the data displayed in the ListView. Take into account that internally ListView creates a pool of itmes (or a pool for each type of item that can be displayed in your case).

For this purpose your adapter needs to implement the following methods:

  • int getItemViewType(int position): Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item. So you need to identify you types.
  • int getViewTypeCount(): Returns the number of types of Views that will be created by getView(int, View, ViewGroup). This is used to create a pool for each type of item.
Moss