I\'ve got a strange problem that drives me crazy. In my android application I customized my own adapter that extends from ArrayAdapter. The items of the ListView to which I
As everyone says, Android recycle the views. What does that mean? Imagine you have 10 items in your list, but only 3 of them are displayed on the screen. When scrolling down, the first will disappear and the forth will appear. The fourth item was already in the Android system, but the fifth was created from the first item, using the same view. In my case, I had a TextView with a default background. But if some condition was met, I change the background.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
if (someCondition) {
holder.textView.setBackground(R.drawable.different_background)
}
...
}
The problem was that I don't set back the default background if the condition is not met (as I thought that the view was created for each item). The problem appeared when the first item had the condition true, so the recycled view was used with the different_background instead of the default background. To avoid the problem, I had to always set both branches of the condition and set the default values as follows:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
if (someCondition) {
holder.textView.setBackground(R.drawable.different_background)
} else {
holder.textView.setBackground(R.drawable.default_background)
}
...
}