自定义 ListView - 自定义参数

Custom ListView - Custom Argument

我正在为公交车站创建一个应用程序,以提供时间表。为此,我正在使用自定义列表视图。这是:

class custom_adapter extends ArrayAdapter<String>{

public custom_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;
}

}

现在你可以看到我只有 2 个 texViews,"hora" 用于显示总线的计时器,"nota" 用于一些注释,比如某天总线不显示去或类似的东西。而我的问题正是在 "nota" textview 上。我有几十个 arrayList 传递给这个自定义 ListView,所以有几十个计时器,有些计时器我需要记下来,有些则不需要。那么,我可以为这个自定义 ListView 设置另一个参数,比如布尔值或其他参数,这样我就可以在该代码中执行 if / else 以在每个参数上添加注释。我需要改变什么才能做到这一点?我一直在努力,但没能成功。

如何制作一个 class 来保存两个值 'hora' 和 'nota',并使用布尔值 isNotaAvailable() 方法。然后在 getView() 中你只需做这样的事情:

@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);

YourClassName singleHorario = getItem(position);
TextView hora = (TextView) costumView.findViewById(R.id.Hora);
TextView nota = (TextView) costumView.findViewById(R.id.Nota);
// set hora text 
hora.setText(singleHorario);
// check if nota is available, if true - set nota text
if(singleHorario.isNotaAvailable()) {
nota.setText(singleHorario.getNota())}
else nota.setVisibility(View.GONE);

return costumView;
}

这只是一个想法,如果有帮助请告诉我:)

只需扩展BaseAdapter,这样就可以定义数据结构了。

class custom_adapter extends BaseAdapter

ArrayList<String> horarios 更改为 ArrayList<Data> horariosData 可以是

public class Data{
    private String hora;
    private String nota;
    private boolean shouldShowNota;

    //write getter and setter here 
}

最后,读取getView

中的数据
Data data = getItem(position);
if (data.getShouldShowNota) {
    nota.setText(data.getNote);
}
hora.setText(data.getHora);

不要使用字符串作为 ArrayAdapter 的参数,而是创建自定义 class 并使用它。
这样你就可以将你想要的所有信息传递到适配器中,并按照你喜欢的方式显示它。

public class custom_adapter extends ArrayAdapter<MyClass>{

public custom_adapter(Context context, ArrayList<MyClass> 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);

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

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

    return costumView;
}

和新的 class

public class MyClass {
    public String hora;
    public String nota;
}