2
votes

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

1
have you thought of creating a listview instead, it can help not to repeat code for instances where you have lots of checkboxes - samir-mangroliya.blogspot.com.cy/2012/09/… - Tasos
yeah, maybe not like the example you provided, i will take a look, thank you =) - user2582318
it looks likes the way to go, have a look around on github, there maybe a library helper to make things even easier for lots of checboxes - Tasos
i remember that i saw a library to handle that, was easy to get and set checkbox, but cant even remember the name and Why i got this library hahahha thank you man - user2582318
@Tasos im getting near =) -> dj-android.blogspot.in/2013/02/… - user2582318

1 Answers

2
votes

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();