I have listview with custom adapter. In each row i have 2 textviews and a button. I want when I click on an item to highlight it and to stay highlighted until i click another item. I tried to do it with list selector but didn’t succeed. Then I tried to do it like this:
public void onClick(View v) {
ListView lvItems = context.lvLists;
for (int i=0; i < lvItems.getChildCount(); i++)
{
lvItems.getChildAt(i).setBackgroundColor(Color.BLACK);
}
v.setBackgroundColor(Color.parseColor("#555555"));
This is the onclick of each row of the listview. It works but I saw that when I scroll down there are more items selected (which I didn’t select). Solution for this?
If you want to save some properties of your ListView items you can’t rely on Views themselves. You have to store them in your data so that your Adapter knows which item’s state has changed.
See the answer to this question for more details:
Android: Spinners within a ListView loose their values when I add dynamically new ListView entries