Reputation: 753
I have lots of checkboxes in my application and i find it inconvenient to check each item if it is selected or not. So i was wondering if there is a way to get only selected and ignore a bunch of unused checkbox.
Right now, my idea is to do something like this
if (cb1.isChecked()) //do something
if (cb2.isChecked()) //do something
...
if (cb32.isChecked()) //do something
But i find it too much to check every checkbox. Is there a way to get only the selected ones?
Example
A CheckBoxGroup wherein you can just do something like checkBoxGroup.getCheckedItems()
I have checked several libraries but it doesnt work this way. Any help will be appreciated, thanks in advance! Cheers!
Upvotes: 0
Views: 939
Reputation: 106
Try setting a single OnCheckedChangeListener
for all the checboxes you have. Like
Checkbox1
CheckBox checkBox1 = findViewById(R.id.check_box1);
checkBox.setOnCheckedChangeListener(checkedChangeListener );
Single OnCheckedChangeListener
for n no of checkboxes:
CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
switch (buttonView.getId())
{
case R.id.check_box1:
if (isChecked)
{
//logic 1
}
else
{
//logic 2
}
;
break;
}
}
};
Handle all the checkbox with in switch.
Upvotes: 2
Reputation:
Use this loop which finds the checkbox by its name:
for (int i = 1; i <= 32; i++) {
int id = getResources().getIdentifier("cb" + i, "id", getPackageName());
CheckBox cb = findViewById(id);
if (cb.isChecked()) {
// your code
}
}
this way you can get a list of all your checked checkboxes by a clicklistener of a button:
final ArrayList<CheckBox> checked = new ArrayList<CheckBox>();
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checked.clear();
for (int i = 1; i <= 32; i++) {
int id = getResources().getIdentifier("cb" + i, "id", getPackageName());
CheckBox cb = findViewById(id);
if (cb.isChecked()) {
checked.add(cb);
}
}
}
});
Now the list checked
contains only the checked checkboxes.
Upvotes: 1
Reputation: 502
You can follow the below steps:
1) First get the reference of the parent of all checkboxes. (It might be instance of any type of layout; like find linearlayout. Say,
LineartLayout parent = (LinearLayout)findViewById(R.id.linearlayout);
2) Then create an Arraylist of String values; say
ArrayList<String> checkedList = new ArrayList();
3) Then run a for loop like
for(int i=0; i<parent.getChildCount(); i++){
if(parent.getChildAt(i) instanceof CheckBox){
CheckBox checkBox = (CheckBox)parent.getChildAt(i);
if(checkBox.isChecked()){
checkedList.add(checkBox.getText());
}
}
}
4) And at the end of for loop, you will get the list of checked values.
Upvotes: 2
Reputation: 1008
I would like to suggest you to create list of checkboxes. then using For loop or Do While loop create new array with only selected checkboxes. By this way in new list you will get only checked checkboxes
Upvotes: 1