6

我用我自己BaseAdapter (standingsListAdapter)ListView (standingsLV)。当我设置适配器时:

standingsLV.setAdapter(standingsListAdapter);

getViewTypeCount()叫做。但后来,当我在适配器中设置新数据时:

standingsListAdapter.setData(data);

setData(data)设置新数据调用后notifyDataSetChanged()

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

不再调用getViewTypeCount()方法。为什么? 如何强制适配器再次检查 getViewTypeCount()?


为什么我需要这个?

  • 我在列表的每一行中显示表格。表格有不同的长度(行数)。
  • 我想在适配器中设置新数据,以便表的长度可以改变

这就是为什么每次新数据到达列表适配器时都应该重新计算我的视图类型的数量。

4

2 回答 2

6

我不认为这是可能的。

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.

于 2013-02-26T21:36:51.623 回答
1

我会尝试:

standingsListAdapter.notifyDataSetChanged();
于 2013-02-26T21:05:36.770 回答