Getview parameter "convertview" not null on new "position" parameter
You need to call bindView()
always. The idea or reuse is as following. If convertView
is null, you create and initialize a new view. If convertView
is not null, you take this view and convert it to be new view, meaning you call bindView()
with convertView
instance.
Checkout this Javadoc for more details.
A friend explained the problem to me and now it seems to work. Basically ListView only holds a small number of views and recycles them all the time. In my case I have a Nexus 4 and so it seems to have 7 views total because the 8th was always the one who started to cause trouble. What I was missing in my getView() was a condition checking for correlation between the position and the ID of the current item within the ArrayAdapter. Here is how it looks now that it works:
@Override
public View getView(int position, @Nullable View convertView, ViewGroup parent) {
View v;
PreviewItemHolder holder = null;
// Initialize view if convertview is null
if (convertView == null) {
v = newView(parent, position);
}
// Populate from previously saved holder
else {
// If position and id of set do not match, this view needs to be re-created, not recycled
if (((PreviewItemHolder) convertView.getTag()).set.getId() != position) {
v = newView(parent, position);
}
else {
// Use previous item if not null
v = convertView;
// Get holder
holder = (PreviewItemHolder) v.getTag();
}
}
// Populate if the holder is null (newly inflated view) OR
// if current view's holder's flag is true and requires populating
if (holder == null || holder.readPopulateFlag()) {
bindView(position, v);
}
return v;
}