This happened because you are using the convertView
and ViewHolder
incorrectly. When you scroll down, the views will be recycled and the converView
will not be null. The ViewHolder
pattern is usually used this way:
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
inflate the view
create a ViewHolder and store the necessary views in it
use setTag to store the ViewHolder in the convertView
}
else {
use getTag to get the ViewHolder
}
update the views // <- you are not doing this when convertView != null
}
The reason that convertView/ViewHolder
pattern is used is that views in a list usually have the same layout but different data shown in them. In essence, the convertView
(when not null) is a view that was previously used in the list and must be converted to show correct data for this position. By reusing it you avoid the expensive layout inflation. The ViewHolder
is a further optimization with which you avoid the also expensive findViewById
calls.