I am implementing an Android app in which I have a list view. Each element of this list view has a toggle button and, using a custom adapter, which extends BaseAdapter, I managed to get which toggle buttons are checked and which are not checked. But, what if I wanted to impose constraints to these toggle buttons? Suppose that when I check the second toggle button, I want that the first one is automatically checked too. How should I do? Thanks
public class MyCustomAdapter<T> extends BaseAdapter {
Context mContext; //the application context
LayoutInflater mInflater; //object that is used to set the layout of the View
ArrayList<T> mList; //list of the items shown in the user interface
SparseBooleanArray mSparseBooleanArray; //object that is used to keep track of the selected items of the list
ToggleButton[] toggleButtons;
public MyCustomAdapter(Context context, ArrayList<T> list) {
this.mContext = context;
mInflater = LayoutInflater.from(mContext);
mSparseBooleanArray = new SparseBooleanArray();
mList = new ArrayList<T>();
this.mList = list;
toggleButtons = new ToggleButton[list.size()];
}
public View getView (int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_with_toggle, null);
}
toggleButtons[position] = (ToggleButton)convertView.findViewById(R.id.toggleEnable);
switch (position){
case 0:
toggleButtons[position].setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(mContext, ""+0, Toast.LENGTH_SHORT).show();
}
});
break;
case 1:
toggleButtons[position].setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Toast.makeText(mContext, ""+isChecked, Toast.LENGTH_SHORT).show();
if(isChecked) {
if(!toggleButtons[0].isChecked()) {
// Toast.makeText(mContext, "elemento 1 "+toggleButtons[1].isChecked(), Toast.LENGTH_SHORT).show();
toggleButtons[0].setChecked(true);
} else {
//things to do if toggleButtons[0] == true
}
} else {
//things to do if isChecked == false
}
}
});
break;
default:
break;
}
return convertView;
}
}