8

I have an exception I never thought I would see. A class cast exception of the adapter when removing a footer view from a ListView (sic).

 java.lang.ClassCastException: com.test.MyAdapter
 at android.widget.ListView.removeFooterView(ListView.java:381)

How can this happen? What does removing a footer have to do with class cast exception????

The list is a multi-list adapter perhaps that is why but still a class cast exception for removing a footer (sic).

Code Droid
  • 10,344
  • 17
  • 72
  • 112

4 Answers4

12

Add your footer view to ListView before calling setAdapter() method.

Added:

public void addFooterView (View v)

Since: API Level 1 Add a fixed view to appear at the bottom of the list. If addFooterView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.

NOTE: Call this before calling setAdapter. This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.

Parameters v The view to add.

Source

Also you can check this interesting post.

Hope this helps.

Community
  • 1
  • 1
Vishal Vyas
  • 2,571
  • 1
  • 22
  • 39
  • Do you know why a class cast exception might occur? – Code Droid Sep 29 '12 at 03:14
  • I have updated my answer with this interesting post http://stackoverflow.com/questions/4393775/android-classcastexception-when-adding-a-header-view-to-expandablelistview I think this will answer your question. – Vishal Vyas Sep 29 '12 at 03:15
  • @CodeDroid if you found my answer correct, can you please accept it. Thanks – Vishal Vyas May 08 '13 at 20:00
  • Funny. On Nexus 5 it works anyway. But on every other phone it only works with this solution! – marcellsimon May 18 '14 at 19:17
  • 1
    This works on a Nexus 5 because the removeFooterView code changed in KitKat. In KitKat the adapter gets wrapped when you call addFooterView, but before KitKat the adapter only gets wrapped when you call setAdapter after you add a footer view. – Rhys Davis Sep 19 '14 at 00:30
9

This is some code for the answer above, it worked in my case:

I had to set a footerView (it's a loadingView in a listView with pagination) to my listView before setting it's adapter and then remove it. First I initialized my loadingView from a layout file in OnCreate method:

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
loadingView = layoutInflater.inflate(R.layout.loading_view, null);

Then I used this workaround in the same method:

this.setIsLoading(true);
listView.setAdapter(adapter);
this.setIsLoading(false);

Where

private void setIsLoading(boolean isLoading)
{
    this.isLoading = isLoading;

    if (isLoading) {
        listView.addFooterView(loadingView);
    }
    else {
        listView.removeFooterView(loadingView);
    }
}
Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70
  • This is what ended up working for me as well. Set a header/footer, initiate the adapter, then remove the header/footer. – wblaschko Jan 27 '14 at 08:19
  • Calling setAdapter again after adding a header/footer is why this fix works – Rhys Davis Sep 19 '14 at 00:31
  • Could you also do a `listView.getAdapter() != null` before adding the footer? – ono Mar 03 '15 at 20:22
  • instead of adding and removing a footerview you could simply change the visibility of "loadingView" between GONE and VISIBLE. Works like a charm – grAPPfruit Apr 03 '15 at 15:43
1

The problem does not come from removeFooterView(), but from addFooterView(). If you read the documentation, it states that an wrapper will be added to your adapter:

If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.

Thus you must use the getter for retrieving the wrapped adapter and cast it to your adapter. Like this:

((MyAdapter) ((HeaderViewListAdapter) listView.getAdapter()).getWrappedAdapter())

Hope this will help you with your issue.

Best regards,

Ionut Negru
  • 6,186
  • 4
  • 48
  • 78
0

Adding to the other answers, if you're adding/removing footers dynamically (such as if they reach the bottom of your list and then you're adding a footer view) the easiest thing is to Override setAdapter in your ListView and add a new View object as the footer there, this will ensure the adapter is wrapped in the HeaderViewListAdapter:

@Override
public void setAdapter(ListAdapter adapter) {
    addFooterView(new View(getContext()));
    super.setAdapter(adapter);
}
ono
  • 2,984
  • 9
  • 43
  • 85