Android开发 ListView页脚更新数据崩溃

ListView如果设置页脚之后(addFooterView),在更新数据的时候有可能会报错

报错内容大致如下:cannot be cast to android.widget.HeaderViewListAdapter

网上找到报错资料

查看代码发现:确实是在ListView 添加adapter之前设置过了页脚。

继续查到资料:java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to com.android.mms 

根据资料修改代码:

	if (foodValue != null ) {
		foodValue.addAll(result);
		if(foodListView.getAdapter() instanceof HeaderViewListAdapter) {
		    HeaderViewListAdapter listAdapter=(HeaderViewListAdapter)foodListView.getAdapter();
		    adapter = (FoodDateAdapter) listAdapter.getWrappedAdapter();
		 }else{
			adapter = (FoodDateAdapter) foodListView.getAdapter(); 
		 }
		adapter.notifyDataSetChanged();
	}

之后,问题解决问题。

---------------------------------------------------------------------------------------

知其然要知其所以然,之所以会这样是因为:

ListView增加HeaderView之后,将原来的Adapter包装成HeaderViewListAdapter,看看HeaderViewListAdapter的文档说明:

ListAdapter used when a ListView has header views. This ListAdapter wraps another one and also keeps track of the header views and their associated data objects.

This is intended as a base class; you will probably not need to use this class directly in your own code.

HeaderViewListAdapter有个方法getWrappedAdapter,该方法能返回被包装的HeaderViewListAdapter的ListAdapter。

到了这里就明白为什么会报ClassCastException异常了。因为ListView的getAdapter方法返回的是HeaderViewListAdapter的实例,而将其转换成BaseAdapter的子类的实例,肯定是不对的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值