【问题标题】:Change text of a listview, that is filled by an ArrayAdapter更改由 ArrayAdapter 填充的列表视图的文本
【发布时间】:2014-05-26 22:22:01
【问题描述】:

有没有办法改变listview中的文本,它由ArrayAdapter填充?

我的数组:

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" 
  }

感谢您的帮助!

【问题讨论】:

  • 你应该使用getItemAtPosition
  • 我应该在哪里使用这个?
  • 我正在研究一个例子。

标签: android listview text android-arrayadapter onitemclicklistener


【解决方案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

  • ArrayAdapters 构造函数使用 android.R.layout.simple_list_item_1 xml 布局作为其第二个参数,并且此布局有一个子级 - TextView,ID 为 android.R.id.text1

【讨论】:

    【解决方案2】:

    我认为其他解决方案是修改您的数据,然后使用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。

    【讨论】:

      猜你喜欢
      • 2013-06-04
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      相关资源
      最近更新 更多