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.

1

6 Answers 6

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();
Sign up to request clarification or add additional context in comments.

5 Comments

My listView.getAdapter() isn't an instance of HeaderViewListAdapter after a footer view was added.
Thanks a lot .I couldnt follow the below approach due to structure of my app
@ono: that would be FooterViewListAdapter i think
Now i am getting android.widget.ExpandableListConnector cannot be cast to *
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);

Comments

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();

Comments

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);

Comments

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);

Comments

-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
    }
}

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.