I need to have even rows as white and odd rows sd grey in my ListView (using ArrayAdapter to populate it). I can’t use different layouts with different background color because if I do the selection doesn’t work. What I do is just use this piece of code in my getView() to check if row is odd or even and apply background depending on condition:
if (position % 2 == 0) {
convertView.setBackgroundDrawable(getResources().getDrawable(R.drawable.dark_item_background));}
But this doesn’t work as expected because the only first visible rows are of correct background color. After scrolling to the bottom (I assume because of recycling mechanism) I have very weird behavior and don’t understand why. Here is what I have for the first items (correct case):

Here is what I have after scrolling:

In
getView()objects are reused for better performance.Thats why you get this weird behavior. To solve your problem just add if – else