Mundi
Mundi

Reputation: 80265

ListView adapter gets confused when convertView is recycled

I have a straight forward BaseAdapter for my ListView. It downloads a JSON feed and displays the data in the rows. There is a ViewHolder which contains the views and a data object called "Story". Everything works just fine.

However, after some scrolling of longer lists, I notice two things.

1) My log shows that the adapter is reloading the feed when scrolling further down. This is strange, as I put the whole JSON array into a variable, so why does it have to reload?

2) More importantly, after some scrolling back and forth, the rows contain the wrong "Story" objects. Here are the relevant parts of the getView routine:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    Story story = stories.get(position);
    if (convertView == null) {
        //create holder
        holder = new ViewHolder();
        convertView = inflator.inflate(R.layout.story_list_item, parent, false);
        holder.titleView = (TextView) convertView.findViewById(R.id.story_list_title);
        holder.dateView = (TextView) convertView.findViewById(R.id.story_list_date);
        holder.story = story;
        holder.imageView = (ImageView) convertView.findViewById(R.id.story_list_image);
        convertView.setTag(holder);
        } else  {
            holder = (ViewHolder) convertView.getTag();
        }       
    // configure the view       
    holder.titleView.setText(story.title);
    return convertView;
}

Simple enough. Now the strange thing is that I can fix the problem by eliminating the if statement if (convertView == null) (and, I presume, eliminating the row recycling as well).

But will I not run into memory problems this way? Why does the plain vanilla version not work?

Thanks for your help. Regards, S

Upvotes: 0

Views: 1766

Answers (1)

Jens
Jens

Reputation: 17077

You are aware that you're only assigning

holder.story = story 

when convertView == null ? Consider moving holder.story = story to just after your convertView if-case and it should work a lot better. Btw, do you even need to store the "story" inside your view holder? Typically that pattern should only be used to store Views and view state information, not the data of the actual position.

Upvotes: 1

Related Questions