The expandablelistview adapter child not updated on runtime by calling notifyDataSetChange

人盡茶涼 提交于 2019-12-11 14:38:27

问题


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 collapsing and expanding again child updated:


回答1:


Can you please try to refresh adapter like

getActivity().runOnUiThread(new Runnable() {
    public void run() {
        this.notifyDataSetChanged();
    }
});



回答2:


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

}


来源:https://stackoverflow.com/questions/50945605/the-expandablelistview-adapter-child-not-updated-on-runtime-by-calling-notifydat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!