gpt4 book ai didi

android - 更改由 ArrayAdapter 填充的 ListView 的文本

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:50 26 4
gpt4 key购买 nike

有什么方法可以更改由 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 与ID android.R.id.text1.

关于android - 更改由 ArrayAdapter 填充的 ListView 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23878422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com