samedi 29 décembre 2018

Add and Remove selected items of a checkbox from java ArrayList in Android

I have a function that dynamicaly builds checkboxes according to the values of an ArrayList,

Now I want to add those items to another ArrayList when checked and Remove them when Uncheck.

Adding when Item is checked is working but when I uncheck the checkbox it gives an error.

public void BuildCheckBox(){

    FinalSeatList.removeAll(FinalReservedSeatList);

    //Build checkboxus
    LinearLayout l1 = (LinearLayout)findViewById(R.id.linear_view);
    for(int i = 0; i < FinalSeatList.size(); i++) {
        final CheckBox cb = new CheckBox(this);
        cb.setText(FinalSeatList.get(i));
        l1.addView(cb);
        final int finalI = i;
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isChecked()) {
                    SelectedSeatList.add(FinalSeatList.get(finalI));
                    Toast.makeText(SelectSeatsActivity.this, "Added: " + SelectedSeatList.get(finalI), Toast.LENGTH_SHORT).show();
                }
                else
                {
                    if(SelectedSeatList.contains(FinalSeatList.get(finalI))){
                        SelectedSeatList.remove(FinalSeatList.get(finalI));
                        Toast.makeText(SelectSeatsActivity.this, "Removed: " + SelectedSeatList.get(finalI), Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(SelectSeatsActivity.this, "Unchecked", Toast.LENGTH_SHORT).show();
                    }


                }
            }

        });
    }




}




Aucun commentaire:

Enregistrer un commentaire