微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐