执行 notifyDataSetChanged 时 ListView 出现 ClassCastException [英] ClassCastException with ListView when executing notifyDataSetChanged

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

问题描述

我在listVivew的头部添加了一个视图,

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.

推荐答案

似乎每当您在列表视图中使用页眉/页脚视图时,您的 ListView 都会被 HeaderViewListAdapter 包裹起来.您可以使用以下代码修复此问题:

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 时 ListView 出现 ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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