Question

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));
 }
Was it helpful?

Solution

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.

OTHER TIPS

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.

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top