lundi 29 avril 2019

How to use multiple OnCheckedChangeListener for two or more groups of Android checkboxes

I am creating an Android application where I use two or more checkbox groups.

I dynamically create checkbox and OnCheckedChangeListener, I need to group the checkboxes in groups to limit the number of checkboxes that should be selected.

I have my class products and each product has extras, for some products there are 1 or more extras. Of which each extra I can select 1 or more ingredients.

The problem is that the Listener works for any checkbox, I can not assign a listener for each group of checkboxes.

How can I implement multiple listeners for each group of checkboxes?

I have already tried to put in a for the creation of a new OnCheckedChangeListener but everything works as a single checkbox group, not as a new one.

         for(int j = 0; j < extras.size(); j++){

            extra = extras.get(j);
            //Si los extras son checkboxs
            final LinearLayout layoutCheckBoxs = new LinearLayout(this);
            ViewGroup.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            ((LinearLayout.LayoutParams) lp).setMargins(16,16,16,16);
            layoutCheckBoxs.setOrientation(LinearLayout.VERTICAL);
            layoutCheckBoxs.setId(j+1);
            layoutCheckBoxs.setTag(j);
            final CheckBox[] checkBoxes = new CheckBox[extra.getListIngredients().size()];

            CheckBox.OnCheckedChangeListener checker = new CheckBox.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton cb, boolean b) {
                    if (count == extra.getMaxSelect() && b) {
                        cb.setChecked(false);
                        Toast.makeText(getApplicationContext(),
                                String.format("Solo puedes seleccionar %s %s",extra.getMaxSelect(),extra.getMaxSelect()==1?"extra":"extras"), Toast.LENGTH_SHORT).show();
                    } else if (b) {
                        count++;
                        Log.e("Count",String.valueOf(count));
                        extrasCompleted[Integer.valueOf(layoutCheckBoxs.getTag().toString())] = count;

                        enabledButtonAddCart(extras);
                    } else if (!b) {
                        count--;
                        Log.e("Count",String.valueOf(count));
                        extrasCompleted[Integer.valueOf(layoutCheckBoxs.getTag().toString())] = count;
                        enabledButtonAddCart(extras);
                    }
                }
            };

            for(i = 0; i < extra.getListIngredients().size(); i++){
                checkBoxes[i] = new CheckBox(this);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    checkBoxes[i].setTextAppearance(R.style.customCheckBoxStyle);
                }else{
                    checkBoxes[i].setTextAppearance(this,R.style.customCheckBoxStyle);
                }
             /*   checkBoxes[i].setTypeface(type);*/
                String rightText = "+ "+fmt.format(extra.getListIngredients().get(i).getPrice());
                String leftText =  extra.getListIngredients().get(i).getName();
                CharSequence text = null;
                if(extra.getListIngredients().get(i).getPrice()!=0) {
                    text = new Truss()
                            .append(leftText)
                            .pushSpan(new LineOverlapSpan())
                            .append("\n")
                            .popSpan()
                            .pushSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE))
                            .pushSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPriceExtras)))
                            .append(rightText)
                            .build();
                }else{
                    text = new Truss()
                            .append(leftText)
                            .pushSpan(new LineOverlapSpan())
                            .append("\n")
                            .popSpan()
                            .build();
                }
                checkBoxes[i].setText(text);
                checkBoxes[i].setId(i);
                checkBoxes[i].setPadding(16,16,16,16);
                //setMargins(checkBoxes[i],15,5,15,5);
                setMargins(checkBoxes[i],15,15,15,15);


                checkBoxes[i].setOnCheckedChangeListener(checker);
}




Aucun commentaire:

Enregistrer un commentaire