Adapter class cast exception when removing a Footer view?

前端 未结 4 1792
不思量自难忘°
不思量自难忘° 2021-01-02 13:51

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.ClassCa         


        
4条回答
  •  长情又很酷
    2021-01-02 14:13

    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,

提交回复
热议问题