ClassCastException with ListView when executing notifyDataSetChanged
Asked Answered
H

6

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.

Hereunto answered 9/12, 2010 at 23:24 Comment(1)
You need to use ListView.LayoutParams. Check [this question][1]. [1]: #4394275Impure
A
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();
Anterior answered 30/5, 2012 at 16:3 Comment(5)
My listView.getAdapter() isn't an instance of HeaderViewListAdapter after a footer view was added.Soggy
Thanks a lot .I couldnt follow the below approach due to structure of my appSimilitude
@ono: that would be FooterViewListAdapter i thinkLurcher
Now i am getting android.widget.ExpandableListConnector cannot be cast to *Track
Not so, see stanllysong.blogspot.ru/2013/08/javalangclasscastexception.html.Magnification
B
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);
Brawny answered 11/8, 2014 at 3:57 Comment(0)
M
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();
Magnification answered 1/4, 2016 at 14:46 Comment(0)
E
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);
Evasion answered 13/12, 2014 at 15:40 Comment(0)
O
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);

Overstudy answered 16/8, 2017 at 22:55 Comment(0)
J
-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
    }
}
Jonell answered 17/4, 2012 at 10:56 Comment(1)
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 betterHagio

© 2022 - 2024 — McMap. All rights reserved.