expandablelistview适配器子代不会在运行时通过调用notifyDataSetChange进行更新 [英] The expandablelistview adapter child not updated on runtime by calling notifyDataSetChange

查看:140
本文介绍了expandablelistview适配器子代不会在运行时通过调用notifyDataSetChange进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

组视图的BaseExpandableListAdapter上有一个按钮,用于在其子级中添加注释.当将该项目添加到列表中并在该适配器类内部调用notifyDataSetChanged()时,它不会立即更改,但是会在折叠和扩展列表之后更改.

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:

推荐答案

实际上,就我而言,expandableListView的高度未更新,因此会导致运行时出现问题.深入研究代码之后.我在notifyDataSetChanged()之后调用此setListViewHeight函数,使我的消耗性列表视图在运行时更新.我将我的expandablelistView的高度设置为:

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

}

这篇关于expandablelistview适配器子代不会在运行时通过调用notifyDataSetChange进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆