0

I'm using a ListView with a custom Adapter and custom item layout where every item has a description with a switch. Depending on what the item is, I give the switch different thumb selector-drawables per code in getView().

   swtch.setThumbDrawable(getContext().getResources()
        .getDrawable(R.drawable.green_switch)); // or red_switch

This works fine, but: When I scroll the ListView, the custom images are gone, until I touch a switch again, then the custom image of it comes back immediately.

When the state list contains an element of the “disabled” state at first, the switch will have this image instead of the image it should.

It's either a bug or an improperly use of the ListAdapter by me, I think.

A workaround for me would be to include switches with different styles in the layout and show one of them depending on the item type I have and hide the others.

Is this the only possibility?

Woalk
  • 146
  • 6

1 Answers1

0

Sound like you have problems with view recycling. You have to set all the fields in the getView each time. Please read How ListView's recycling mechanism works. Understanding view recycling is very important when using ListViews.

Community
  • 1
  • 1
userM1433372
  • 5,345
  • 35
  • 38
  • I'll read and try some things with this, thank you, maybe it clears up some things, even when I know of most parts of this already. My question floating in my head is that I think when I set the Drawable in `getView`, it **is** set everytime, or is it not? – Woalk Aug 04 '14 at 11:26