java Android - 如何更改现有 ListView 的页眉/页脚视图元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7893697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:52:22  来源:igfitidea点击:

Android - How to change header/footer view elements for existing ListView?

javaandroid

提问by Jake Wilson

Say I add a header view to my list view using the typical method like so:

假设我使用如下典型方法将标题视图添加到列表视图中:

View header = getLayoutInflater().inflate(R.layout.list_header, null);
TextView headerText = (TextView) header.findViewById(R.id.my_textview);
headerText.setText("This is my header!");

myListView.addHeaderView(header);
myListView.setAdapter(adapter);

Then, later I need to alter the text of the header textview...

然后,稍后我需要更改标题文本视图的文本...

TextView headerText = (TextView) findViewById(R.id.my_textview);
headerText.setText("new header text!");

This doesn't appear to work, since the way I originally attached the header to the list was by inflating it...

这似乎不起作用,因为我最初将标题附加到列表的方式是通过膨胀它...

How do I change the text?

如何更改文本?

采纳答案by Craigy

You should simply store the reference to headerText that you used originally. Then call setTexton it later.

您应该简单地存储对您最初使用的 headerText 的引用。然后setText稍后调用它。

回答by Kurtis Nusbaum

Where are you executing the code to change the header text? If your not doing it on the UI thread the textview won't update.

你在哪里执行代码来更改标题文本?如果您不在 UI 线程上执行此操作,则 textview 将不会更新。