Reputation: 63
I have button for every ListView item. Every button begins green or gray, depending on a JSON array. If the button begins green then once it is clicked it turns gray. If a button begins gray once its clicked it turns green. The problem is when I scroll up and out of view of the button and then back to the button it has turned back to its original color. Here is the code for my buttons,
if(p.JSONarray().equals("NO")){
button.setBackgroundResource(R.drawable.gray);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View button) {
button.setBackgroundResource(R.drawable.green);
}//end on click
});
}//end if equals NO
if(p.JSONarray().equals("YES")){
button.setBackgroundResource(R.drawable.green);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View button) {
button.setBackgroundResource(R.drawable.gray);
}//end on click
});
}//end if equals yes
Why does the button go back to its original color once it goes out of view?
Upvotes: 1
Views: 90
Reputation: 1748
You need to put an else condition to maintain the color of the button. In your case your code should be like this:
if(p.JSONarray().equals("YES")){
button.setBackgroundResource(R.drawable.green);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View button) {
button.setBackgroundResource(R.drawable.gray);
}//end on click
});
}else
{
button.setBackgroundResource(R.drawable.gray);
}
Upvotes: 0
Reputation: 13520
The color change back to default because the views are created again. You will need to maintain a list of Button
positions you have already clicked so that when you reach that position again in getView
you can change the color of your Button
Upvotes: 1