ClassCastException with ListView when executing notifyDataSetChanged

前端 未结 6 1249
醉酒成梦
醉酒成梦 2020-12-08 19:11

I have added a view to the header of listVivew,

    View TopSearch =  (View) View.inflate(this, R.layout.search, null);
    lv.addHeaderView(TopSearch, null         


        
6条回答
  •  鱼传尺愫
    2020-12-08 19:47

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

提交回复
热议问题