自定义列表视图不显示正确的内容

时间:2016-07-07 00:13:03

标签: android listview

在那里我已经为我的列表视图制作了一个习惯,但我不知道它以这种方式显示它的原因:

Display

这是我生成列表视图上使用的列表的方式:

final ArrayList<String> list = db_query.get_marks();
    final ArrayList<String> list_trim = db_query.get_trim();
    final ArrayList<String> list_merged = new ArrayList<String>();
    for (int i=0; list.size()>i; i++){
        list_merged.add(list.get(i));
        list_merged.add(list_trim.get(i));
    }
    String[] list_final = new String[list_merged.size()];
    this.size = list.size();
    list_merged.toArray(list_final);
    ListAdapter adapter = new Custom_Adapter(this, list_final);

所以我应该展示的是:AB AB,但实际上显示AA BB

图像中的7个值是A

图像中的1个值是B

自定义视图代码:

 LayoutInflater inflater = LayoutInflater.from(getContext());
        View customView = inflater.inflate(R.layout.custom_row, parent, false);


        TextView text1 = (TextView) customView.findViewById(R.id.textView4);
        TextView text2 = (TextView) customView.findViewById(R.id.textView5);

        text1.setText((String) getItem(position));

        text2.setText((String) getItem(position));



        return customView;

希望你能帮助我

  public Custom_Adapter(Context context, ArrayList<Model> list) {
    super(context, R.layout.custom_row, list);

}

1 个答案:

答案 0 :(得分:0)

您要设置两次相同的值

text1.setText((String) getItem(position));
text2.setText((String) getItem(position));

制作模型文件Model.java:

public class Model {
    public String a;
    public String b;
}

替换此代码:

final ArrayList<String> list = db_query.get_marks();
final ArrayList<String> list_trim = db_query.get_trim();
final ArrayList<Model> list_merged = new ArrayList<Model>();
for (int i = 0; i < list.size(); i++){
    Model model = new Model();
    model.a = list.get(i);
    model.b = list_trim.get(i);
    list_merged.add(model);
}
ListAdapter adapter = new Custom_Adapter(this, list_final);

最后这个:

text1.setText((String) ((Model)getItem(position)).a);
text2.setText((String) ((Model)getItem(position)).b);

更改适配器以接受Model对象。