【问题标题】:The expandablelistview adapter child not updated on runtime by calling notifyDataSetChange通过调用 notifyDataSetChange 未在运行时更新可扩展列表视图适配器子
【发布时间】:2018-06-20 10:06:57
【问题描述】:

在组视图的 BaseExpandableListAdapter 上有一个按钮,用于在其子项中添加 cmets。将项目添加到列表并在该适配器类中调用 notifyDataSetChanged() 时,它不会立即更改,而是在折叠和展开列表后更改。

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

}

点击添加评论按钮子后未更新:

再次折叠和展开后子更新:

【问题讨论】:

    标签: java android expandablelistview expandablelistadapter notifydatasetchanged


    【解决方案1】:

    你可以尝试刷新适配器吗

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

    【讨论】:

    • 它在 addCaseHearingsToAdapter 函数中的位置吗?
    • addCaseHearingsToAdapter() 函数在 expandableAdapterClass 中,所以这个 getActivity() 函数在这个可扩展适配器类中没有解析
    • 我正在这样做 new Handler().post(new Runnable() { public void run() { notifyDataSetChanged(); } });但它不再工作了
    • 我也检查了这个链接但是stackoverflow.com/questions/6374378/…但是什么也没找到
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多