I know it sounds very simple, and there are questions about this. But none of it could solve my problem. So here we go:
I want to change background color of a list item in a ListActivity
when user clicks on it, and change it back to original color when user clicks again (i.e. Select/Unselect item sort of look)
I tried using getChildAt, it works perfectly if I have all the items visible in one screen without having to scroll.
Code:
getListView().getChildAt(position).setBackgroundColor(Color.CYAN);
The problem begins when I have more items in the list and user has to scroll through them. Once background for an item is changed, The background color shows up on the newly visible items as I scroll. Also, the getChildAt(position)
returns null
(and hence a NullPointerException
) when clicking again on the item.
Can anyone please help me with a simple code that helps me change background color of a list item?
Thanks in advance!