I have added a view to the header of listVivew,
View TopSearch = (View) View.inflate(this, R.layout.search, null);
lv.addHeaderView(TopSearch, null
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);
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();
@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);
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
}
}
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();
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);