0

When I'm using parent.getChildAt(position).setBackgroundColor(Color.GRAY); in my public void onItemClick(AdapterView<?> parent, View view, int position, long id) it colorizes, but it works strange.

When I click first or second item it colorizes it... and every item away ~five records. Sometimes I've got NullPointerException. Completely weird, because position is unique and it should recieve me appropriate View, but it doesn't.

I saw solution with overriding getView method, but I'm using this adapter in different places. I just want to color clicked item. How to get reference to selected view?

EDIT:

In my adapter class I created:

public static int selectedItem = -1;

I added this to my overrided getView method:

    if(selectedItem == position){
        parent.getChildAt(position).setBackgroundColor(Color.GRAY);
    }

In my activity I added that:

myAdapter.selectedItem = position;
myAdapter.notifyDataSetChanged();

And It doesn't work. Where I do a mistake?

KurdTt-
  • 449
  • 1
  • 5
  • 21
  • try this: http://stackoverflow.com/questions/16976431/change-background-color-of-selected-item-on-a-listview – balaji koduri Feb 16 '15 at 11:59
  • you can try this way [you can go through this way.click here....][1] [1]: http://stackoverflow.com/questions/2562051/listview-item-background-via-custom-selector?rq=1 – MPG Feb 16 '15 at 12:09

2 Answers2

1

It's not a bug - it's the way ListView re-uses the views to save resources.

So to avoid this behavior you should on every getView() set all used attributes for all your views.

Updated - to be quite clear
In your particular case it means that you should set color like this:
1) In onItemClick() - in your actitivity - you should remember given position as selected:

myAdapter.selectedItem = position

2) In getView() - in your adapter:

if(selectedItem == position)
    parent.getChildAt(position).setBackgroundColor(Color.GRAY);
else
    parent.getChildAt(position).setBackgroundColor(0);//or whatever defauld color

Update 2
If you want to select many items you should use some structure (like HashSet) to hold all the selected items:
1) In your activity class add member:

public static HashSet<Integer> mSelectedItems = new HashSet<Integer>();

2) In onItemClick() use following to flip selected state:

if(mSelectedItems.contains(position))
   mSelectedItems.remove(position);
else 
   mSelectedItems.add(position);

3) In getView():

   if(MainActivity.mSelectedItems.contains(position))
        parent.getChildAt(position).setBackgroundColor(Color.GRAY);
    else
        parent.getChildAt(position).setBackgroundColor(0);//or whatever defauld color
sberezin
  • 3,266
  • 23
  • 28
  • I don't get your point. Could you post any code? Which attributes? – KurdTt- Feb 16 '15 at 11:59
  • A bit. So: In [this](http://stackoverflow.com/questions/16976431/change-background-color-of-selected-item-on-a-listview) link, there is a _mSelectedItem_ variable. Is it a static field of adapter class? I don't get it at all. – KurdTt- Feb 16 '15 at 12:45
  • m prefix is somekind of convention for class member variables. In that example they are storing the last selected item position by updating that variable. That's irrelevant for your problem. – Juanjo Vega Feb 16 '15 at 13:29
  • @KurdTt, I've updated code once more using your variable names. The crucial thing - you should add **else** clause to make your code work well – sberezin Feb 16 '15 at 13:47
  • @sberezin Well, It works, but It colorize only **one** item. I want to colorize many items. When I'm leaving _else_ section It works like before. Can I do this? – KurdTt- Feb 16 '15 at 15:13
0

At first read this article;

Then use ViewHolder pattern;

And try to setBackgroundColor() in onItemClick() like this:

    view.setBackgroundColor(0);//or whatever defauld color
GIGAMOLE
  • 1,274
  • 1
  • 11
  • 17