3

Possible Duplicate:
Adapter class cast exception when removing a Footer view?

Here is the log:

java.lang.ClassCastException: com.test.MyAdapter cannot be caste to  android.widget.HeaderViewListAdapter
at android.widget.ListView.removeFooterView(ListView.java:387)
at com.test.MyActivity.removeFooterViews()

MyAdapter is a MultiList Adapter. Don't really see what removing a footer has to do with casting my adapter to android.widget.HeaderViewListAdapter? Where does this come from?

Here is all that happens in MyActivity.removeFooterViews()

       if (myFooterView != null)
        {    
                   myListView.removeFooterView(myFooterView);  
        }
Community
  • 1
  • 1
Code Droid
  • 10,344
  • 17
  • 72
  • 112

1 Answers1

4

Can we see the code that leads to this rather than just the exception message? If I had to guess I would say that you're calling something along the lines of ListView.getAdapter() and casting the returned ListAdapter to the type used when you ListView.setAdapter().

This is a common mistake. However, it's important to remember that the ListAdapter you set is not the same one that the ListView returns to you since it uses adapters under the hood to support headers and footers.

zienkikk
  • 2,404
  • 1
  • 21
  • 28
  • The code that leads to it is myListView.removeFooterView(myFooterView) thats it. – Code Droid Oct 04 '12 at 00:13
  • I think that's part of the problem. When you're asking the ListView to remove a footer, it assumes that the footer had been set first. If the footer is set then the ListView's internal adapter will be of type HeaderViewListAdapter so it will cast to that inside removeFooterView(). However, if you haven't set a footer, then the ListView will hold on to your MyAdapter instance. Thus, the conversion fails. – zienkikk Oct 04 '12 at 00:17
  • the listView count is 1 so something is in there. – Code Droid Oct 04 '12 at 00:19
  • when I add the footer more than once I get several copies inside. When I call this the list count is 1 and I think the footer is there. – Code Droid Oct 04 '12 at 00:19
  • It's worth mentioning that if you're going to add a footer or header view, it has to be done _before_ calling `setAdapter` or, as @zienkikk has mentioned, it won't be a `HeaderViewListAdapter` and thus won't support header or footer views. – Jason Robinson Oct 04 '12 at 00:21
  • I see. How can I verify that setAdapter has not already been called? I set it more than once. Do I need to do a clear? – Code Droid Oct 04 '12 at 00:23
  • it gets reset because I have several different footers that I add/remove per different scenarios. – Code Droid Oct 04 '12 at 00:25
  • I don't think you should be calling `setAdapter(...)` more than once. But try using `setAdapter(null)` in between your "resets." I have no idea what that will do but it may work for you. – zienkikk Oct 04 '12 at 00:27
  • It depends. If you still have header and footer views attached to the `ListView`, calling `setAdapter(null)` will still make a new `HeaderViewListAdapter`. You need to remove all your header and footer views before calling `setAdapter()` again. – Jason Robinson Oct 04 '12 at 00:33