lundi 5 octobre 2020

Remove selected item id and value when CheckBox is unchecked from RecyclerView

I've a RecyclerView with CheckBox in it and the data is populated via Web Services. I'm able to get the selected value when the checkbox is checked. But when I un-check the checkbox the App crashes. The exception that I get is Index out of bound exception Index: 8, Size: 2.

This the the recyclerview adapter class onBindViewHolder method. here items is the list Object that contains the values.

PhysicianSpinnerAdapter countrySpinnerViewHolder = (PhysicianSpinnerAdapter) holder;
    PopulateDoctorSpecializationListItem populateCountryListItem = items.get(position);
    countrySpinnerViewHolder.binding.spinnerItem.setText(populateCountryListItem.getsName());
    countrySpinnerViewHolder.binding.checkItem.setId(populateCountryListItem.getId());
    countrySpinnerViewHolder.binding.checkItem.setChecked(items.get(position).isSelected);
    countrySpinnerViewHolder.binding.checkItem.setTag(items.get(position));
    countrySpinnerViewHolder.binding.checkItem.setOnClickListener(v -> {
        CheckBox checkBox = (CheckBox) v;
        PopulateDoctorSpecializationListItem absentListItem = (PopulateDoctorSpecializationListItem) checkBox.getTag();
        absentListItem.setSelected(checkBox.isChecked());
        if (((PhysicianSpinnerAdapter) holder).binding.checkItem.isChecked()) {
            items.get(position).setSelected(checkBox.isChecked());
            onItemClick.onItemCheck(populateCountryListItem);
        } else {
            onItemClick.onItemUnCheck(populateCountryListItem);
        }
    });

This is the implementation of it in the Activity Class. The issue occurs in onItemUnCheck method

                @Override
                public void onItemCheck(PopulateDoctorSpecializationListItem spinnerItems) {
                    selectedItems.add(spinnerItems.getsName());
                    Log.d(TAG, spinnerItems.getsName());
                    selectedItemID.add(spinnerItems.getId());
                }

                @Override
                public void onItemUnCheck(PopulateDoctorSpecializationListItem spinnerItems) {
                    for (PopulateDoctorSpecializationListItem items : doctorSpecializationListItem) {
                        selectedItems.remove(items.getsName());
                        selectedItemID.remove(items.getId());
                        /*for (int i = 0; i < spinnerItems.size(); i++) {
                            selectedItemID.remove(i);
                        }*/
                    }
                }



Aucun commentaire:

Enregistrer un commentaire