Friends,
I am trying to write a ExpandableListView which use single choice checkboxes at ChildView. And I can't understand how to set other CheckBoxes to "false" in OnChildClickListener() of ExpandableListView. Here is my code:
ExpListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox);
if (cb.isChecked()) {
} else {
cb.setChecked(true);
//Here somehow I must set all other checkboxes to false.
//Is it possible?
}
return false;
}
});
here is xml of ChildView :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textChild"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textColor="@android:color/white"
android:layout_weight="1"
/>
<CheckBox android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"
android:layout_gravity="right"
android:visibility="visible"
/>
</LinearLayout>
If you want to only be able to select one checkbox, you could store the checked checkbox in a variable CheckBox checkedBox;
. When a CheckBox
is clicked, you could do something along the lines of
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
CheckBox last = checkedBox //Defined as a field in the adapter/fragment
CheckBox current = (CheckBox) v.findViewById(R.id.checkbox);
last.setCheked(false); //Unchecks previous, checks current
current.setChecked(true); // and swaps the variable, making
checkedBox = current; // the recently clicked `checkedBox`
return false;
}
Though, I'm not sure if this will work with Androids view recycle system, but it's worth a shot.
If you need multiple choices, you could expand the checkedBox
into a List<CheckBox>
, and iterate over it each time you need to uncheck boxes.
If you need to store some additional data (which you most likely need to), you could make a holder class, e.g.
class CheckBoxHolder{
private CheckBox checkBox:
private int id;
public CheckBoxHolder(CheckBox cb, int id){
this.checkBox = cb;
this.id = id;
}
// Getter and/or setter, etc.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With