android get selected checkboxes in a group

10,246

I have made a custom view named CheckBoxGroupView:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.CheckBox;
import android.widget.GridLayout;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


public class CheckBoxGroupView extends GridLayout {

    List<CheckBox> checkboxes = new ArrayList<>();

    public CheckBoxGroupView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void put(CheckBox checkBox) {
        checkboxes.add( checkBox);
        invalidate();
        requestLayout();
    }

    public void remove(Integer id) {
       // TODO: Remove items from ArrayList
    }

    public List<?> getCheckboxesChecked(){

        List<CheckBox> checkeds = new ArrayList<>();
        for (CheckBox c : checkboxes){
            if(c.isChecked())
                checkeds.add(c);
        }

        return checkeds;
    }

    public List<Object> getCheckedIds(){

        List<Object> checkeds = new ArrayList<>();
        for (CheckBox c : checkboxes){
            if(c.isChecked())
                checkeds.add(c.getTag());
        }
        return checkeds;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        for(CheckBox c: checkboxes) {
            addView(c);
        }

        invalidate();
        requestLayout();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }
}

This is an example for a layout that uses CheckBoxGroupView:

<com.example.views.CheckBoxGroupView
    android:id="@+id/cbGroup"
    android:columnCount="2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</com.example.views.CheckBoxGroupView>

Setting and getting the Ids:

CheckBoxGroupView cbGroup = (CheckBoxGroupView) findViewById(R.id.cbGroup);
Checkbox cb = new CheckBox(this);
cb.setTag(1);
cb.setText("Banana");
cbGroup.put(cb);
cbGroup.getCheckedIds().toString();
Share:
10,246
user2582318
Author by

user2582318

Updated on June 04, 2022

Comments

  • user2582318
    user2582318 about 2 years

    I am implementing something like a category, so a product can be in 1 or more categories

    now i set a Tag for the CheckBox like

      <CheckBox
                    android:id="@+id/c1"
                    android:tag="checkbox1"
                    android:text="Title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
    

    and go on

    when the user press to save i check if each checkbox is selected than add the tag to a arraylist and convert it to String separed by comma and save

    when i need to get the list and set the selected checkbox, i receive the String from db, split into a String[] then iterate over it, find the element in android using the getResource, and then set it to select=true.

    I see that like a huge job, i saw sometime ago(cant remember) a kind of group of checkbox you just use groupOfCheckBox.getSelected();

    if you selected the number one and number 2 it return 1,2..

    and to set it checked was easy as groupofCheckBox.setSelect("1,2")

    but i cant find it anymore, someone know if i was dreaming about it or really exist some way to do that