0

在我的应用程序中,我使用的是 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.

如何修复?

4

2 回答 2

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

谢谢,埃亚尔。

于 2013-05-30T14:21:39.120 回答
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));
}
于 2013-05-30T13:58:03.450 回答