3

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:

  1. Added header and/or footer.
  2. Then set adapter.

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.

  1. A standard Activity where the List is the only view displayed.
  2. A Fragment inside a ViewPager

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);
Community
  • 1
  • 1
Bam
  • 478
  • 6
  • 19

3 Answers3

3

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.

Community
  • 1
  • 1
Daniel Wilson
  • 18,838
  • 12
  • 85
  • 135
1

I found a slight cheating way around this issue.
The question is still an issue and it's still unanswered.

What can be done is to add any header or footer desired during list construction (before adding adapter) and then running remove header/footer view after running the adapter.
This does add some extra work, specially if the headers or footers require any inflating or layout management, but it does fix the issue.

Example Code below:

addHeaders();
addFooters();

public void setAdapter(ListAdapter adapter)
{
    super.setAdapter(adapter);

    // Fix for issues with ListView, Fragment and header/footer
    hideAllHeaders();
    hideAllFooters();
}
Bam
  • 478
  • 6
  • 19
1

Before setting adapter to listview, add dummy view as header or footer view. Then set adapter to listview. This makes listview to instance of headerviewslist. Then you can add and remove footers easily from listview as normal.

sathish
  • 11
  • 7