ClassCastException with ListView when executing notifyDataSetChanged
Solution 1
It seems that whenever you use header/footer views in a listview, your ListView gets wrapped with a HeaderViewListAdapter. You can fix this using the below code:
((YourAdapter)((HeaderViewListAdapter)lv.getAdapter()).getWrappedAdapter()).notifyDataSetChanged();
Solution 2
API 18 and lower is confused about what it is wrapping. To help it, set your header and/or footer PRIOR to assigning the adapter. That way the correct wrapping takes place under the covers. Then remove the header/footer immediately after (if that is what you want).
myList.addFooterView(myFooterView);
myList.setAdapter(adapter);
myList.removeFooterView(myFooterView);
Solution 3
As written in http://stanllysong.blogspot.ru/2013/08/javalangclasscastexception.html it should be done so:
HeaderViewListAdapter hlva = (HeaderViewListAdapter)l.getAdapter();
YourListAdapter postAdapter = (YourListAdapter) hlva.getWrappedAdapter();
postAdapter.notifyDataSetChanged();
bobetko
Updated on June 02, 2022Comments
-
bobetko almost 2 years
I have added a view to the header of listVivew,
View TopSearch = (View) View.inflate(this, R.layout.search, null); lv.addHeaderView(TopSearch, null, false);
And everything is fine until I try to execute (when data changes)
adapter.notifyDataSetChanged();
That always crash my application giving me following error:
> java.lang.ClassCastException: android.widget.HeaderViewListAdapter
If I remove header view then there is no error. Any suggestions? Thanks.