【问题标题】:ArrayAdapter with class带类的 ArrayAdapter
【发布时间】:2015-10-27 00:58:18
【问题描述】:

所以伙计们,在我的项目中,我使用的是自定义 listView,起初我只传递给适配器字符串,但现在我想传递类,因为我想传递更多信息。但我不知道为什么它不会让我! 这是我的带字符串的适配器:

class costum_adapter extends ArrayAdapter<String>{

    public costum_adapter(Context context, ArrayList<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;
    }
}

现在,我想为了接收类,我只需要更改参数,ArrayList horarios 在哪里,我将更改为 ArrayList horarios。 “horario”是我的课,在这里:

public class horario{
    public String hora;
    public int destino;
    public int note;

    public horario(String horaIn, int Destino, int Nota){
        hora = horaIn;
        destino = Destino;
        note = Nota;
    }
}

我也在使用 ArrayList。但是我用类中的对象填充了 ArrayList。 当我尝试更改适配器上的参数以接收我的类时,它无法识别它,我开始输入“horario”但它无法识别,我不知道为什么,因为我在另一个文件中有那个类。

【问题讨论】:

  • 您是否注意到您的构造函数中有错字? costum_adapter 而不是 custom_adapter。
  • 是的,我知道,那是因为当我创建 java 文件时,我发音错误,然后我必须用 costum_adapter 声明所有内容,否则会出错。
  • 确保你像公共类扩展ArrayAdapter一样正确扩展

标签: java android arraylist android-arrayadapter


【解决方案1】:

试试这样:

class costum_adapter extends ArrayAdapter<horario> {

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

【讨论】:

  • 正如我在描述中解释的那样,这是有道理的,我试过了。但问题是它无法识别我的班级。如果我更改为“ArrayList”,则会在“horario”上出现错误,因为它无法识别它
  • 如果问题是它无法识别 horario,那么只需将其导入您的班级。
  • 在哪里导入?怎么样?
  • 到您的适配器类文件。只需检查 horario 属于哪个包,然后将其导入您的适配器。
  • 你能用代码写出你想要解释的东西吗?我不明白:x
【解决方案2】:

试试这个

public costum_adapter(Context context, ArrayList<horario> 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).getHora();
    String singlenote = getItem(position).getNote();
    TextView hora = (TextView) costumView.findViewById(R.id.Hora);
    TextView nota = (TextView) costumView.findViewById(R.id.Nota);

    hora.setText(singleHorario);
    nota.setText(singlenote);

    return costumView;
}

模型类

public class horario{
public String hora;
public int destino;
public int note;

public horario(String horaIn, int Destino, int Nota){
    hora = horaIn;
    destino = Destino;
    note = Nota;
}

public String getHora() {
    return hora;
}

public void setHora(String hora) {
    this.hora = hora;
}

public int getDestino() {
    return destino;
}

public void setDestino(int destino) {
    this.destino = destino;
}

public int getNote() {
    return note;
}

public void setNote(int note) {
    this.note = note;
}
}

【讨论】:

  • 正如我在描述之前和描述中对答案所说的那样。我已经尝试将“ArrayList”更改为“ArrayList”,显然这就是我需要的。但问题是,当我在那里写 horario 时,它无法识别它,给出错误“无法解析符号 horario”。它不认识我的班级,我不知道为什么
【解决方案3】:

所以我设法解决了我自己的问题。我的问题是我无法将“ArrayList”更改为“ArrayList”,horario 是我想要的课程,但那是因为“horario”是在更大的课程中声明的。所以我只需要更改为“ArrayList”,“mostraHorario”是更大的类。

【讨论】:

    猜你喜欢
    • 2014-04-15
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    相关资源
    最近更新 更多