首页 > 解决方案 > 通过调用 notifyDataSetChange 未在运行时更新可扩展列表视图适配器子

问题描述

组视图上的 BaseExpandableListAdapter 上有一个按钮,用于在其子项中添加评论。将项目添加到列表并在该适配器类中调用 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();

}

单击添加评论按钮后,子项未更新:
单击添加评论按钮后,子项未更新

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

标签: javaandroidexpandablelistviewexpandablelistadapternotifydatasetchanged

解决方案


你能尝试刷新适配器吗

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

推荐阅读