2015-10-26 150 views
0

我正在为公交车站创建应用程序,以提供时间表。为此,我使用自定义列表视图。那就是:自定义ListView - 自定义参数

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然而,“霍拉”是显示总线的定时器,在“诺塔”是一些笔记,就像有一天公共汽车不去或者类似的东西一样。而我的问题正是在于“nota”textview。我有几十个arrayList传递给这个自定义的ListView,以及几十个和几十个定时器,并且有一些定时器需要放置一个便条,其他的我不需要。所以,我可以给这个自定义ListView另外一个参数,就像一个布尔值或者其他东西,所以我可以在该代码中做一个if/else来为每一个添加一个注释。为了做到这一点,我需要改变什么?我一直在努力,但并没有完全做到这一点。

回答

0

读取数据而不是使用字符串为您ArrayAdapter参数,创建一个自定义类并使用它。
通过这种方式,您可以将所有想要的信息传递到适配器,然后按照您的喜好显示。

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

而且新类

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

但问题是,如果我必须传递一个类,那么这个类将不得不拥有ArrayList,但是我想放入的“nota”不是对所有Array的单个注释,而是一个注释, (或不)在数组的每一个项目中。不知道我能否更好地解释。如果我要放一个音符,我传递的数组中的每一项都将被检查。 –

0

如何让持有两个值'hora'和'nota'的类以及可以说boolean 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; 
} 

这只是一个想法,告诉我,如果它可以帮助:)

+0

但问题是,如果我必须通过一个类,这个类必须有它的ArrayList中,但“诺塔”我希望把不是所有数组的单个音符,这是一个笔记,我把(或不)放在数组的每一个项目中。不知道我能否更好地解释。如果我要放一个音符,我传递的数组中的每一项都将被检查。 –

0

只是延长BaseAdapter,这样你就可以定义数据结构。

class custom_adapter extends BaseAdapter 

变化ArrayList<String> horariosArrayList<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); 
+0

但问题是,如果我必须传递一个类,那么类将不得不具有ArrayList,但是我想放入的“nota”不是对所有Array的单个注释,而是一个注释, (或不)在数组的每一个项目中。不知道我能否更好地解释。如果我要放一个音符,我传递的数组中的每一项都将被检查。 –