我正在尝试将一个简单的视图作为页脚添加到 ListView 中。
这样做会引发ClassCastException
. ("cannot be cast to android.widget.HeaderViewListAdapter
")
现在,在您将其标记为无数其他问题的重复之前,请继续阅读,您会明白我为什么寻求帮助。
我了解向文档添加页眉或页脚的必要程序ListView
在奇巧之前。这程序 https://stackoverflow.com/questions/24700588/i-am-using-the-listview-add-remove-footer-for-listview-cross-app-in-android-vers必须:
- 添加页眉和/或页脚。
- 然后设置适配器。
这使得ListView.setAdapter
可以确定适配器是否必须包裹在HeaderViewListAdapter
,基于是否存在任何页眉或页脚。
这很简单,也很容易理解。
我的问题是,即使我遵循此过程,我也会抛出异常!
我在两个不同的地方使用 ListView。
- 列表是唯一显示的视图的标准活动。
- ViewPager 内的 Fragment
我在这两种情况下都遵循相同的过程,首先添加页眉/页脚,然后设置适配器。
In the Activity
这工作正常,一切都按预期进行。
In the Fragment
,我得到一个ClassCastException
thrown.
下面是启动两个实例的代码:
Activity
and Fragment
:
EndlessList list = (EndlessList) rootView.findViewById(R.id.EndlessListArea);
// Adds the Loading and Error footer
list.setupLoadingView();
list.setupErrorView();
// Sets up the Adapter for the list (View presentation and data management).
list.setAdapter(new listAdapter());
我需要帮助弄清楚为什么该程序适用于Activity
,但抛出异常Fragment
.
注意:我什至尝试将适配器实际手动包装在HeaderViewListAdapter
然后将其传递给setAdapter
方法,然后添加页眉/页脚。这消除了异常,但不会向我的片段添加页眉/页脚。代码如下:
HeaderViewListAdapter adp = new HeaderViewListAdapter(null, null, adapter);
super.setAdapter(adp);