在我的应用程序中,我正在使用ListView。 在运行期间,我必须更改列表的标题。 我正在尝试使用以下代码执行此操作 -
if (mGroupMembersList != null) {
mGroupMembersList = null;
MTListView tempListView = (MTListView) this.findViewById(R.id.groupMembersList);
tempListView.addHeaderView(getHeaderView(headerTitle));
tempListView.setAdapter(groupMembersAdapter);
mGroupMembersList = tempListView;
}
但是当它到达
时tempListView.addHeaderView(getHeaderView(headerTitle));
它抛出
java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
如何解决?
答案 0 :(得分:0)
由于您尚未提供完整代码,因此无法告诉您如何修复现有代码。 以下是我在列表视图中实现标题的方法
//add header to list
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.list_header, (ViewGroup)
findViewById(R.id.header_layout_root));
lv.addHeaderView(header, null, false);
//add some list items
String listItems[] = {"List Item One", "List Item Two", "List Item Three", "List
Item Four", "List Item Five"};
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,
listItems));
}
答案 1 :(得分:0)
我找到了问题的答案。 这是我的解决方案 -
String headerTitle = groupsVector.get(activeGroupIndex - 1).name + " "
+ getString(R.string.view_group_members);
if (mGroupMembersList.getHeaderViewsCount() == 0) {
mGroupMembersHeaderView = getHeaderView(headerTitle);
mGroupMembersList.addHeaderView(mGroupMembersHeaderView);
}
if (mGroupMembersList != null) {
TextView titleTextView = (TextView) mGroupMembersHeaderView.findViewById(R.id.text);
titleTextView.setText(headerTitle);
mGroupMembersList.setAdapter(groupMembersAdapter);
}
谢谢, 的Eyal。