组视图的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();
}
答案 0 :(得分:0)
能否请您尝试刷新适配器,如
getActivity().runOnUiThread(new Runnable() {
public void run() {
this.notifyDataSetChanged();
}
});
答案 1 :(得分:0)
实际上,就我而言,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();
}