Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException with ListView when executing notifyDataSetChanged

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.

like image 675
bobetko Avatar asked Dec 09 '10 23:12

bobetko


People also ask

What is notifyDataSetChanged in ListView?

It tells the ListView that the data has been modified; and to show the new data, the ListView must be redrawn.

What happens when we call notifyDataSetChanged?

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.


1 Answers

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(); 
like image 150
Mark Pazon Avatar answered Sep 23 '22 23:09

Mark Pazon