【发布时间】:2013-06-09 07:57:47
【问题描述】:
我有一个列表视图。 listView 的内容存储在适配器中维护的列表中。我想在单击按钮时添加和删除此 listView 的标题。当我添加标题时,我得到适配器已经设置的异常。还有其他方法吗?请帮助。
【问题讨论】:
我有一个列表视图。 listView 的内容存储在适配器中维护的列表中。我想在单击按钮时添加和删除此 listView 的标题。当我添加标题时,我得到适配器已经设置的异常。还有其他方法吗?请帮助。
【问题讨论】:
在 ListView 上,您必须在 .setAdapter() 之前调用 .addHeader() 和 .addFooter()。我的建议是在需要时modify your adapter to display a header row as a first row,而不是使用 .addHeader()。您可能必须在适配器中为此添加新的行类型。此外,为您的适配器添加方法,使您能够隐藏/显示此标题行,但不要忘记在此之后调用 .notifyDatasetChanged()。
【讨论】:
添加Header后必须设置适配器,像这样
View header = getLayoutInflater().inflate(R.layout.header, null);
ListView listView = getListView();
listView.addHeaderView(header);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,
android.R.id.text1, names));
【讨论】:
只需先在 listview 上设置页眉和页脚,然后设置适配器。
喜欢,(记住我只是建议你程序,方法可能与我在这里写的不同。)
// stuff of header
listview.addHeaderView(View v);
//stuff of footer
listview.addFooterView(View v);
//stuff of adapter
listview.setAdapter(mAdapter);
【讨论】: