0

I have a ListView that I am filling with different views depending on the index. This works fine by overriding the getView method, but I am wondering if this is the best way of accomplishing this? The main reason I ask is when I scroll some of the views seem to get screwed up, such as having the wrong background that I set.

Maybe it would help if someone could explain what convertView is exactly and the correct way of implementing it. Because I am concerned that the reason why my views are getting the wrong background is because I am using the convertView coming in to the function and it is not correct.

user
  • 86,916
  • 18
  • 197
  • 190
Bobbake4
  • 24,509
  • 9
  • 59
  • 94
  • 1
    I recommend my answer on [How a list works](http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list/7739006#7739006). Maybe this will help you to understand lists and adapters better... – Knickedi Oct 25 '11 at 14:29

2 Answers2

2

When you are recycling views you need to realise that the current convertView (when convertView != null) you are dealing with is already in a certain state (e.g. has a certain background for a different element). Therefore you need to make sure you set every part of the view accordingly and assume no default values.

Think of it this way, when you are scrolling downwards, the top view is moved out of visibility and reused as the new view appearing at the bottom - if you don't change anything for this view, it will look exactly the same as before.

Che Jami
  • 5,151
  • 2
  • 21
  • 18
1

Regarding convertView maybe this part from the Commonsware books might help you(page 107 if you want to jump).

user
  • 86,916
  • 18
  • 197
  • 190
  • Thanks for the link, the Commonsware book was very helpful in explaining how ListViews work and how getView should be used. I fixed my issues, thanks. – Bobbake4 Oct 25 '11 at 16:07