Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-如何更改现有ListView的页眉/页脚视图元素?_Java_Android - Fatal编程技术网

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

Java Android-如何更改现有ListView的页眉/页脚视图元素?,java,android,Java,Android,假设我使用以下典型方法将标题视图添加到列表视图中: 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); myLi

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

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);
然后,稍后我需要更改标题文本视图的文本

TextView headerText = (TextView) findViewById(R.id.my_textview);
headerText.setText("new header text!");
这似乎不起作用,因为我最初将标题附加到列表的方式是通过膨胀它


如何更改文本?

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

您只需存储对最初使用的headerText的引用即可。然后稍后对其调用
setText

为什么第二次获取标题textview结果代码textview headerText=(textview)findViewById(R.id.my_textview)?只需直接使用第一个设置text.post整个活动代码,这样我们就可以轻松理解问题。这就是我最终采用的方法。但是因为
headerText
是从膨胀视图中检索的,所以我还必须存储对膨胀视图的引用。但是它成功了!