38

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.

bobetko
  • 5,019
  • 14
  • 58
  • 85
  • You need to use `ListView.LayoutParams`. Check [this question][1]. [1]: http://stackoverflow.com/questions/4393775/android-classcastexception-when-adding-a-header-view-to-expandablelistview/4401369#4401369 – Macarse Oct 14 '11 at 17:48

6 Answers6

125

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();
Mark Pazon
  • 6,167
  • 2
  • 34
  • 50
12

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);
Nebu
  • 1,352
  • 1
  • 14
  • 21
6

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();
CoolMind
  • 26,736
  • 15
  • 188
  • 224
0

@mussharapp's answer is perfectly right and it works! However I find more convenient to simply cache your adapter on a member variable for later use before you do setAdapter():

mAdapter = new YourAdapter(ctx, items);
listView.addFooterView(v);
listView.setAdapter(mAdapter);
Carlos Ginés
  • 1,192
  • 1
  • 7
  • 9
0

The reason for class cast exception is that the listview did't wrapped to headerlistview. So we can't add footers or header to listview directly. So before setting adapter to listview, add dummy view as header or footer view. Then set adapter to listview. This makes listview to instance of headerviewslist. Then you can add and remove footers easily from listview as normal.

listview.addFooterView(new View(mContext));listview.setAdapter(yourAdapter): 

After setting adapter, you can add or remove footer listview.addFooterView(yourFooter); or listview.removeFooterView(yourFooter);

sathish
  • 11
  • 7
-2
public class YourOwnList extends ListActivity {
    private EfficientAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        mAdapter = new EfficientAdapter(/*your parameters for the adapter*/);
    }

    private void yourMethod () {
        mAdapter.notifyDataSetChanged();
    }

    private static class EfficientAdapter extends CursorAdapter {
        // your adapter
    }
}
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
  • 2
    Welcome to StackOverflow. It is always recommended to add a line or two about the code you are posting, it helps the fellow members to understand your code better – Vivek Oct 28 '12 at 18:20