【问题标题】:How to get values from a ArrayList in one class and use them in another class如何从一个类中的 ArrayList 获取值并在另一个类中使用它们
【发布时间】:2017-05-31 13:32:15
【问题描述】:

我正在尝试从另一个类中的 Arraylist 中检索值,在我拥有的第一个类中:

public ArrayList<HashMap<String, String>> getListaCategorias() {
    return listaCategorias;
}

现在在我的另一个类中,我试图获取 ArrayList 中的值并将它们插入到 TextView 中:

TodasAsCategorias todasAsCategorias = new TodasAsCategorias();
todasAsCategorias.getListaCategorias();

TextView txtDesignacao = (TextView)convertView.findViewById(R.id.Designacao);
txtDesignacao.setText("Values from the ArrayList");

问题是从 ArrayList 中获取值并将它们插入到 TextView 中,想法是有一个 ListView,其中每个值都指向一个项目(txtDesignacao 是位于 list_item 内的 TextView),我不能真正使用 SimpleAdapter,因为我的 ListView 作为一些特殊属性,这就是我使用 CustomAdapter 的原因

编辑

我将 arraylist 声明为静态,但现在我有一个错误:

TodasAsCategorias todasAsCategorias = array.get(i);

ArrayList<HashMap<String, String>> array = TodasAsCategorias.getListaCategorias();
TodasAsCategorias todasAsCategorias = array.get(i);
TextView txtDesignacao = (TextView) convertView.findViewById(R.id.Designacao);
txtDesignacao.setText("Values from the ArrayList");

说不兼容的类型,需要类TodasAsCategorias,找到HashMap

【问题讨论】:

  • 一个选项是静态变量或方法
  • 你想从一个 ArrayList 中设置一个文本组件?
  • @davidxxx 是的,这正是我想要的

标签: java android arraylist custom-adapter


【解决方案1】:
ArrayList<HashMap<String, String>> array = getListaCategorias();
TodasAsCategorias todasAsCategorias = array.get(i);
TextView txtDesignacao = (TextView) convertView.findViewById(R.id.Designacao);
txtDesignacao.setText("Values from the ArrayList");

【讨论】:

  • 但我想将arraylist中的值设置为TextView
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
相关资源
最近更新 更多