Search code examples
javaandroidexpandablelistviewexpandablelistadapternotifydatasetchanged

The expandablelistview adapter child not updated on runtime by calling notifyDataSetChange


There is a button on BaseExpandableListAdapter on group view for adding comments in its child. When adding the item to the list and called notifyDataSetChanged() inside that adapter class, it is not changing instantly but changed after collapsing and expanding the list.

  private List<String> ParentItem;

  private LinkedHashMap<String, List<JsonCase>> ChildItem;

  public View getChildView(final int listPosition, final int expandedListPosition,boolean isLastChild, View convertView, final ViewGroup parent) {
    final JsonCase expandedListText = (JsonCase) getChild(listPosition, expandedListPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = layoutInflater.inflate(R.layout.hearing_detail,parent, false);

    }

        final EditText comment = (EditText) convertView.findViewById(R.id.comment);
        TextView commentorName = (TextView) convertView.findViewById(R.id.commentorName);
        TextView commentDate = (TextView) convertView.findViewById(R.id.commentDate);
        comment.setText("" + expandedListText.details);
        if(expandedListText.commentorName!=null &&expandedListText.commentorName.equals("")){
            commentorName.setEnabled(false);
        }else{
            commentorName.setText(expandedListText.commentorName);
        }

        commentDate.setText(expandedListText.date);

    return convertView;
}

public void addCaseHearingsToAdapter(int listPosition){

    String hearingId = this.ChildItem.get(this.ParentItem.get(listPosition)).get(0).hearingId;
    String userId = PreferenceData.getLoggedInUserId(context);
    JsonCase comment = new JsonCase();
    comment.commentId = "";
    comment.hearingId = hearingId;
    comment.commentedBy = userId;
    comment.details = "";
    comment.commentorName = "";
    comment.date =  new SimpleDateFormat("dd MMMM yyyy hh:mm:ss").format(Calendar.getInstance().getTime());

    this.ChildItem.get(this.ParentItem.get(listPosition)).add(comment);
    this.notifyDataSetChanged();

}

After clicking add comment button child not updated:
After clicking add comment button child not updated

After collapsing and expanding again child updated:
After collapsing and expanding again child updated


Solution

  • Actually, in my case, the height of expandableListView is not updated so it causes a problem on runtime. After digging into code. I call this setListViewHeight function after notifyDataSetChanged(), makes my expendable list view updated on runtime. I set the Height of my expandablelistView as:

       public static void setListViewHeight(ExpandableListView listView, int group) {
        ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
        int totalHeight = 0;
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
                View.MeasureSpec.EXACTLY);
        for (int i = 0; i < listAdapter.getGroupCount(); i++) {
            View groupItem = listAdapter.getGroupView(i, false, null, listView);
            groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
    
            totalHeight += groupItem.getMeasuredHeight();
    
            if (((listView.isGroupExpanded(i)) && (i != group)) || ((!listView.isGroupExpanded(i)) && (i == group))) {
                for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                    View listItem = listAdapter.getChildView(i, j, false, null,
                            listView);
                    listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
    
                    totalHeight += listItem.getMeasuredHeight()+1;
    
                }
            }
        }
    
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        int height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
        if (height < 10)
            height = 200;
        params.height = height;
        listView.setLayoutParams(params);
        listView.requestLayout();
    
    }