有什么方法可以更改由 ArrayAdapter 填充的 ListView 中的文本?
我的数组:
public String[] trainingstage = {"Hello", "Hello 2"};
数组适配器
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, trainingstage);
setListAdapter(adapter);
ListView listView = getListView();
listView.setOnItemClickListener(this);
在项目上:
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
//untrained
case 0:
//here the text in the listview should change from "Hello" to "BYE"
case 1:
//here the text in the listview should change from "Hello 2" to "BYE 2"
}
感谢您的帮助!
最佳答案
你可以这样实现你想要的:
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView tv = (TextView)view.findViewById(android.R.id.text1);
switch (position) {
//untrained
case 0:
//here the text in the listview should change from "Hello" to "BYE"
tv.setText("BYE");
break;
case 1:
//here the text in the listview should change from "Hello 2" to "BYE 2"
tv.setText("BYE 2");
break;
}
}
什么是android.R.id.text1
ArrayAdapter
的构造函数使用android.R.layout.simple_list_item_1
xml 布局作为它的第二个参数,这个布局有一个 child -TextView
与IDandroid.R.id.text1
.
关于android - 更改由 ArrayAdapter 填充的 ListView 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23878422/