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.
It tells the ListView that the data has been modified; and to show the new data, the ListView must be redrawn.
From the docs notifyDataSetChanged() : Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With