Reputation: 58
I needed to correlate checkboxes with their position in the list view in the checkboxes' onClickListener. My first solution was to make a very short custom view that extended checkbox but had an extra variable (position) that would be set in getView().
public class ListCheckBox extends CheckBox {
private int position = -1;
public ListCheckBox(Context context)
{super(context);}
public ListCheckBox(Context context, AttributeSet attrs)
{super(context,attrs);}
public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr)
{super(context,attrs,defStyleAttr);}
@TargetApi(21)
public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{super(context,attrs,defStyleAttr,defStyleRes);}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
It worked, except it changed the checkboxes' color to black, and nothing I did (including changing android:buttonTint) could change it. For now my solution is a HashMap with a view key and integer value that keeps track of the checkboxes' and their positions, but if anyone has a less ugly solution or any idea as to why I couldn't change the color of the checkboxes, it would be very much appreciated.
Upvotes: 3
Views: 525
Reputation: 26
You can use the method of setTag() to past a extra variable to a view,then you can use getTag() to get the extra variable. This does not need to custom CheckBox.
//set tag
checkBox.setTag(position);
//get tag
int position = (int)checkBox.getTag();
Upvotes: 1