jaume5900 jaume5900 - 5 months ago 8
Android Question

Custom listview not displaying the right content

Hellow there i've made a custom adapted for my listview but i dont know the reason why its displaying it in this way:

Display

This is how i generate the list that i use on listview:

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


So what i should be displaying is : AB AB but its actually displaying AA BB

7 values in the image are A

1 values in the image are B

The custom view code:

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;


Hope you can help me

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

}

Answer

You're setting the same value twice

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

Make a model file Model.java:

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

Replace this code:

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

And finally this:

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

Change you adapter to accept the Model object.