I have created a custom ArrayAdapter to display data within ListView items from an array of
data structures and for some reason when I scroll up and down really quickly the display will do strange things.
Here is my getView function from the custom ArrayAdapter. My first guess would be because of the if (d.highTemp != null) is causing lag or something? What I want the adapter to do is only display a highTemp if the data structure has a value within it. Otherwise it should display the lowTemp value; The list is correct when initially drawn but if I scroll up and down really fast it will start displaying both high and lowTemp values even though I know each data structure only has one of those != null…
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
DaysWeather d = daysWeather[position];
if (d != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
TextView trt = (TextView) v.findViewById(R.id.toprighttext);
TextView tbt = (TextView) v.findViewById(R.id.bottomrighttext);
if (tt != null) {
tt.setText(d.name);}
if(bt != null){
bt.setText(d.shortDescription);
}
if (d.highTemp != null){
if (trt != null) {
trt.setText("Hi: " + d.highTemp);
tbt = null;
}
} else {
if (tbt != null) {
trt = null;
tbt.setText("Lo: " + d.lowTemp);
}
}
}
return v;
}
Since you are reusing any given row’s view, that row may already have the Hi temp text view set, so you need to explicitly set the field that you don’t want to an empty string (or hidden).
You need to do something like this: