Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView, BaseAdapter, getViewTypeCount() - How to force adapter to check again getViewTypeCount()?

I'm using my own BaseAdapter (standingsListAdapter) for the ListView (standingsLV). When I set the Adapter:

standingsLV.setAdapter(standingsListAdapter);

getViewTypeCount() is called. But later, when I set new data in adapter:

standingsListAdapter.setData(data);

where setData(data) after setting new data calls notifyDataSetChanged():

public void setData(StandingsModelWrapper data) {
    groupsData.clear();
    if (data != null) {
        groupsData.addAll(data.getModel());
    }
    notifyDataSetChanged();
}

the method getViewTypeCount() isn't called again. Why? How to force adapter to check again getViewTypeCount()?


Why do I need this?

  • I display tables in every row of the list. Tables have different lenght (the number of rows).
  • I want to set new data in the adapter so lenght of tables can change

That is why the number of my viewtypes should be recalculated every time new data comes to the list adapter.

like image 308
AppiDevo Avatar asked Feb 26 '13 20:02

AppiDevo


1 Answers

I don't think this is possible.

Get getViewTypeCount() implementation is part of the definition of your adapter; it defines, regardless of what the actual data of the adapter is at any given moment, all the possible types of data it can hold.

getViewTypeCount is called by the framework to properly recycle list-item views that could be very different between different types of the data held by the adapter. When you change data in the adapter, views are still recycled according to the existing definition/implementation of the getViewTypeCount.

If you want a getViewTypeCount to be called again, create a brand new adapter and set this new one as your listview's adapter.

like image 139
Streets Of Boston Avatar answered Sep 20 '22 16:09

Streets Of Boston