【问题标题】:ArrayList in an adapter?适配器中的 ArrayList?
【发布时间】:2016-01-23 10:51:51
【问题描述】:

所以伙计们,我将尝试解释一下我想做的项目的这一部分,以便你们更好地理解我的问题。我正在尝试不为公交车站开发应用程序来显示时间表。为了展示它,我使用了一个 ListView,一个服装。如果我只将字符串传递给适配器,它就可以工作。但是为了只在一个字符串数组中做会占用大量内存,因为我必须构建很多数组。因为,试着想象一下,我有离开某个地方的公共汽车的时间表,但是然后,根据用户选择的目的地,我必须从数组中排除一些项目,因为并非所有公共汽车的终点都在同一个地方,有些赢了'不要去选定的目的地,所以我必须把那些拿出来。因此,如果我打算只在多个阵列中执行此操作,则必须为出发和目的地的每一个可能性构建数十个阵列。所以我想用一个数组列表来做,这样会更容易删除我不想要的项目。 现在的问题是我无法发送到costum listview和arraylist的适配器,像这样:

ArrayList<String> horarios= new ArrayList<String>();
ListAdapter horarioAdapter = new costum_adapter(this, horarios);
ListView horarioListView = (ListView) findViewById(R.id.horario_listView);
horarioListView.setAdapter(horarioAdapter);

它在第二行给了我错误,因为我不能把“horarios”放在适配器中,因为它是一个数组列表。我该如何解决这个问题?希望你们明白我想要做什么。 抱歉描述太长了。

这是我的 costum_adapter,没有太多,只有 2 个 textView,我只是想试试这个,然后添加一些东西。

class costum_adapter extends ArrayAdapter<String>{

public costum_adapter(Context context, String[] horarios) {
    super(context, R.layout.costum_listview ,horarios);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater horarioInflater = LayoutInflater.from(getContext());
    View costumView = horarioInflater.inflate(R.layout.costum_listview, parent, false);

    String singleHorario = getItem(position);
    TextView hora = (TextView) costumView.findViewById(R.id.Hora);
    TextView nota = (TextView) costumView.findViewById(R.id.Nota);

    hora.setText(singleHorario);
    nota.setText(" ");
    return costumView;
}

}

【问题讨论】:

  • 请显示你的costum_adapter构造函数。
  • 你应该只使用数据库/contentProvider和游标适配器,不需要大数组
  • 已经发布了构造函数 ChrisStillwell
  • 我在 java 和 androidStudio 中有点新的编程,我不太确定你指的是什么。出血182

标签: java android arrays listview arraylist


【解决方案1】:

修复适配器构造函数中的参数

public costum_adapter(Context context, ArrayList<String> horarios) {
    super(context, R.layout.costum_listview ,horarios);
}

【讨论】:

  • 谢谢,真是个愚蠢的错误——'但是,嘿,我能问问你对我在做什么的看法吗?你认为我想要 arrayList 工作正常还是有另一种方法可以更轻松地完成这项工作?
  • @CésarPereira 据我了解,您希望在每次用户需要其他数据时以及更新适配器后更改 ArrayList。如果是这样,在这种情况下使用ArrayList 是很常见的。为了提供更准确的答案,需要更多关于应用程序及其 UI 的信息。但是,我想你的方向是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多