其他分享
首页 > 其他分享> > android – 更改列表视图的文本,由ArrayAdapter填充

android – 更改列表视图的文本,由ArrayAdapter填充

作者:互联网

有没有办法更改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" 
  }

感谢帮助!

解决方法:

你可以像这样实现你想要的:

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

> ArrayAdapters构造函数使用android.R.layout.simple_list_item_1 xml布局作为其第二个参数,此布局有一个子项 – 带有ID android.R.id.text1的TextView.

标签:android,text,listview,android-arrayadapter,onitemclicklistener
来源: https://codeday.me/bug/20190708/1404834.html