执行notifyDataSetChanged时ClassCastException异常带的ListView [英] ClassCastException with ListView when executing notifyDataSetChanged

查看:172
本文介绍了执行notifyDataSetChanged时ClassCastException异常带的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个以listVivew的头,

 查看卓=(查看)View.inflate(这一点,R.layout.search,NULL);
    lv.addHeaderView(至卓飞高,空,假);

和一切都很好,直到我试图执行(当数据发生变化)

  adapter.notifyDataSetChanged();

这总是崩溃我的应用程序给我以下错误:

> java.lang.ClassCastException:android.widget.HeaderViewListAdapter

如果我删除头看法则没有错误。有什么建议么?谢谢你。


解决方案

看来,每当你在ListView使用页眉/页脚的意见,你的ListView被包裹着HeaderViewListAdapter。你可以解决这个使用下面的code:

<$p$p><$c$c>((YourAdapter)((HeaderViewListAdapter)lv.getAdapter()).getWrappedAdapter()).notifyDataSetChanged();

I have added a view to the header of listVivew,

    View TopSearch =  (View) View.inflate(this, R.layout.search, null);
    lv.addHeaderView(TopSearch, null, false);

And everything is fine until I try to execute (when data changes)

adapter.notifyDataSetChanged();

That always crash my application giving me following error:

> java.lang.ClassCastException: android.widget.HeaderViewListAdapter

If I remove header view then there is no error. Any suggestions? Thanks.

解决方案

It seems that whenever you use header/footer views in a listview, your ListView gets wrapped with a HeaderViewListAdapter. You can fix this using the below code:

((YourAdapter)((HeaderViewListAdapter)lv.getAdapter()).getWrappedAdapter()).notifyDataSetChanged();

这篇关于执行notifyDataSetChanged时ClassCastException异常带的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆