Sunday 17 December 2017

ExpandableListView - notifyDataSetChanged() - Tricky

In general, we use to call the notifyDataSetChanged() method for updating the list view with our new data sets i.e. objects with either array list or any collections. This data set updations made in course of load more or pull to refresh etc.

While using the listview we can simply update the data-set object and call the notifyDataSetChanged() method from the adapter. It will take care the necessary action of updating the listview.  But this will not been happens in the case of ExpandableListView, hence it requires few thread based handling and again need to set the updated adapter to the ExpandableListView.  Please find below the code snippet for the same.


if (dataList.size() > 0) {
 if (!isShowMore) {
  dataListGlobal = dataList;
  dataListExpandableAdapter = new DataListExpandableAdapter(getActivity(), dataListGlobal);
  expandabaleDataListView.setAdapter(dataListExpandableAdapter);
 } else {
  dataListGlobal.addAll(dataList);
  getActivity().runOnUiThread(new Runnable() {
   public void run() {
    if (dataListExpandableAdapter != null) {
     dataListExpandableAdapter.notifyDataSetChanged();
     expandabaleDataListView.setAdapter(dataListExpandableAdapter);
    }
   }
  });
 }
}

Happy Coding :-)

No comments: