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

标签 android listview text android-arrayadapter onitemclicklistener

有什么方法可以更改由 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/

相关文章:

windows - 批量字符串问题(搜索整行)

android - 获取 ListView / View 中第一个项目的可见高度

android - 地址类 getCountryCode() 方法返回数字

android - 如何正确使用Intent

java - 改造:发送 POST 请求

java - Android - 将一个项目从一个ListView添加到另一个ListView?

android - ListView 中的自定义复选框

Mysql 在一列中搜索文本

file - 如何在 Groovy 中抓取一行的一部分

java - 无法在onActivityResult()中提示视频