有没有办法更改listview中的文本,该文件由ArrayAdapter填充?
我的阵列:
public String[] trainingstage = {"Hello", "Hello 2"};
ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, trainingstage);
setListAdapter(adapter);
ListView listView = getListView();
listView.setOnItemClickListener(this);
OnItem:
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"
}
感谢您的帮助!
答案 0 :(得分:1)
你可以达到你想要的效果:
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布局作为其第二个参数,此布局有一个孩子 - TextView
ID为android.R.id.text1
。答案 1 :(得分:0)
我认为其他解决方案是修改您的数据,然后使用notifyDataSetChanged
。例如:
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:
trainingstage[position] = "BYE 1"; // or trainingstage[0]
adapter.notifyDataSetChanged();
break;
case 1:
trainingstage[position] = "BYE 2"; // or trainingstage[1]
adapter.notifyDataSetChanged();
break;
}
}
由于您已经过滤了该位置,因此可以将trainingstage[position]
替换为trainingstage[0]
或1。