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:
- Added header and/or footer.
- 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.
- A standard Activity where the List is the only view displayed.
- 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);