本文介绍了向ListView throws添加页脚不能转换为android.widget.HeaderViewListAdapter“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图添加一个简单的View作为ListView的页脚。
这样做会抛出一个 ClassCastException 。 (不能转换为android.widget.HeaderViewListAdapter



在将此标记为无数的其他问题,请继续阅读,您将明白为什么我要求帮助。



我了解添加页眉或页脚到 ListView 在KitKat之前。 必须是:

这样, ListView.setAdapter 可以根据是否存在任何页眉或页脚,确定适配器是否必须包装在 HeaderViewListAdapter 中。
这是简单易懂的。



我的问题是,即使我按照这个过程我得到了异常抛出!



我使用我的ListView在两个不同的地方。

我在两者中遵循相同的过程实例,首先添加标题/页脚,然后设置适配器。



活动按照预期工作。

Fragment 中,我收到一个 ClassCastException 抛出。 >

以下是启动两个实例的代码:



活动片段

  EndlessList list =(EndlessList)rootView。 findViewById(R.id.EndlessListArea); 

//添加载入和错误页脚
list.setupLoadingView();
list.setupErrorView();

//设置列表的适配器(查看演示和数据管理)。
list.setAdapter(new listAdapter());

我需要帮助,找出为什么程序与活动,但使用 Fragment 引发异常。




注意:我甚至尝试手动将适配器包装在一个 HeaderViewListAdapter 中,然后将其传递给 setAdapter 方法,然后添加页眉/页脚。这样可以摆脱异常,但是不会为我的Fragment添加页眉/页脚。代码如下:

  HeaderViewListAdapter adp = new HeaderViewListAdapter(null,null,adapter); 
super.setAdapter(adp);


解决方案

可能没有足够的代码来回答这个问题,但是您是否知道如果将列表中的页眉或页脚添加到适配器中,则适配器将自动包装在HeaderViewListAdapter中?



请参阅:

 ((YourAdapter)((HeaderViewListAdapter)listView.getAdapter())。getWrappedAdapter())。 notifyDataSetChanged(); 

如果您在添加页眉或页脚后尝试访问列表的适配器,则此异常将被抛出。


I'm attempting to add a simple View as footer to a ListView.Doing so throws a ClassCastException. ("cannot be cast to android.widget.HeaderViewListAdapter")

Now before you mark this as a duplicate of countless other questions, read on and you will understand why I'm asking for help.

I understand the necessary procedure for add header or footer to a ListView before KitKat. The procedure has to be:

This is so that the ListView.setAdapter can determine if the adapter has to be wrapped in a HeaderViewListAdapter, based on whether there exists any headers or footers.This is simple and easy to understand.

My issue is that even if I follow this procedure I get the Exception thrown!

I use my ListView in two different places.

I follow the same procedure in both instances, first add the header/footer, then set the adapter.

In the Activity this works fine and everything works as expected.
In the Fragment, I get a ClassCastException thrown.

Below is the code for the initiation of both instances:

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());

I need help figuring out why the procedure work with the Activity, but throws an exception with the Fragment.


NOTE: I have even attempted to actually manually wrap the adapter in a HeaderViewListAdapter and then pass it to the setAdapter method, and then add the header/footer. This gets rid of the exception, but does not add a header/footer to my Fragment. Code below:

HeaderViewListAdapter adp = new HeaderViewListAdapter(null, null, adapter);
super.setAdapter(adp);
解决方案

There is probably not enough code to answer this question, but are you aware that if you add a header or footer to a list, the adapter automatically gets wrapped in a HeaderViewListAdapter?

See the answer in this question:

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

If you are trying to access the adapter of the list after adding a header or footer, this exception will get thrown.

这篇关于向ListView throws添加页脚不能转换为android.widget.HeaderViewListAdapter“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 08:42
查看更多