ajp
ajp

Reputation: 45

How to set the background color of an item in a list view

I have a list view full of items that contain multiple widgets each such as imageviews and checkboxes. I have an array adapter and a filter for search functionality. The problem i have is i want to check the check boxes and have something happen. What i did for my test was to check the check box that exists at the top item in the list view and have it print out its state. I noticed that this worked but when i scrolled down , that other check boxes were selected as well. I did some research and i realized that it is a feature of listview to reuse and be well optimized and thus the checked checkbox was being reused. I want to know is it possible to have me check a check box in one of my items in my list, and have the corresponding item turn a color and not have other items turn the same color when i scroll.

So far i have tried setbackgroundcolor and setimagebitmap on the views linear layout, and both have the above results.

Thank you

Upvotes: 0

Views: 373

Answers (3)

Ranjitsingh Chandel
Ranjitsingh Chandel

Reputation: 1489

If you used adapter class. Then you can add onItemClickListner in getView brackets. Is available in adapter class. And using View object and item position set color for that.

Upvotes: 0

user468311
user468311

Reputation:

What you need - is to store checkbox state in some sort of array. Save position and state in it, and then in your adapter set view state according to array item in position.

Upvotes: 0

kabuko
kabuko

Reputation: 36302

Really the way you want to do it is to keep your view and data code better separated. You should have a field on your data item that is reflected in the checkbox and/or background color. This means that when you check your checkbox, you shouldn't just change the background color directly, but instead change the value of the underlying data item. Also, every time you bind your data item to your view, you should set the checkbox based on this field value.

Upvotes: 1

Related Questions