Android 从RecyclerView中取消选中复选框时删除所选项目id和值
我有一个带有复选框的RecyclerView,数据通过Web服务填充。选中复选框后,我可以获得所选的值。但当我取消选中复选框时,应用程序崩溃。我得到的异常是索引越界异常索引:8,大小:2 这是recyclerview适配器类onBindViewHolder方法。这里items是包含值的列表对象Android 从RecyclerView中取消选中复选框时删除所选项目id和值,android,checkbox,android-recyclerview,Android,Checkbox,Android Recyclerview,我有一个带有复选框的RecyclerView,数据通过Web服务填充。选中复选框后,我可以获得所选的值。但当我取消选中复选框时,应用程序崩溃。我得到的异常是索引越界异常索引:8,大小:2 这是recyclerview适配器类onBindViewHolder方法。这里items是包含值的列表对象 PhysicianSpinnerAdapter countrySpinnerViewHolder = (PhysicianSpinnerAdapter) holder; PopulateDocto
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);
}
});
这是它在Activity类中的实现。问题发生在检查方法中
@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);
}*/
}
}
@覆盖
public void onItemCheck(已填充的八月份专用列表项spinnerItems){
选择EdItems.add(spinnerItems.getsName());
Log.d(标记,spinnerItems.getsName());
选择editemid.add(spinnerItems.getId());
}
@凌驾
公共检查(已填充的八月份专业列表项目喷丝头项目){
for(PopulatedOctorSpecializationListItems:doctorSpecializationListItem){
选择editems.remove(items.getsName());
选择editemid.remove(items.getId());
/*对于(int i=0;i