0

I am trying to change the background color by a click on ListView. My code looks like this:

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {          
        arg1.setBackgroundColor(Color.GREEN);
    }

The strange thing is this, if I click on Item number 7, the background of Item 7 changes but also from Item number 6.

Boe-Dev
  • 1,585
  • 2
  • 14
  • 26

1 Answers1

3

Its because ListView recycles the views (row items) used inside. The best solution would be to change background color of your desired row/item by overriding getView method in your adapter. By this, you'll have more control over your list items and you may reset the background color for other unwanted views. For more info, read this

waqaslam
  • 67,549
  • 16
  • 165
  • 178