如何在一个列表中获取“获取所有复选框”值类型可验证

时间:2019-04-29 10:10:13

标签: java android android-recyclerview recycler-adapter android-checkbox

在以下代码中,我要将所有checkboxes值存储在

List<integar>ProcedureIDs = new ArrayList<>();

但是它仅保存上次选中的复选框ID。我想在其中保存多个ID ...

这是我的recycleView适配器。

public class ProcedureAdapter extends  RecyclerView.Adapter<ProcedureAdapter.MyViewHolder> {

    private List<LabAppointmentProcedure> labAppointmentProcedureList;
       List<Integer> ProcedureIDs = new ArrayList<>();
    class MyViewHolder extends  RecyclerView.ViewHolder implements View.OnClickListener{
        TextView procedureName, procedureId;
        private int procedureID;
        CheckBox checkBox;

        MyViewHolder(final View itemView){
            super(itemView);
            itemView.setOnClickListener(this);

            procedureName = (TextView) itemView.findViewById(R.id.txtProcedureName);
            procedureId = (TextView) itemView.findViewById(R.id.txtProcedureId) ;
            checkBox = (CheckBox) itemView.findViewById(R.id.chkProcedure);

            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                         ProcedureIDs.add(Integer.parseInt(procedureId.getText().toString()));
                       // AppointmentDetails.procedureID = Integer.parseInt(procedureId.getText().toString());
                        //EnterSample.ProcedureID = Integer.parseInt(procedureId.getText().toString());
                        EnterSample.ProcedureSampleIDs =ProcedureIDs;
                    }else{
                    }
                }
            });
        }

        @Override
        public void onClick(View v) {

        }
    }

    public ProcedureAdapter(List<LabAppointmentProcedure> labAppointmentProceduresList){
        this.labAppointmentProcedureList = labAppointmentProceduresList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i){
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.procedure_row_item,viewGroup,false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position){
        LabAppointmentProcedure labAppointmentProcedure = labAppointmentProcedureList.get(position);
        holder.procedureName.setText(labAppointmentProcedure.getProcedure());
        holder.procedureId.setText(String.valueOf(labAppointmentProcedure.getProcedureID()));

        if(holder.checkBox.isChecked()){
            holder.procedureID = labAppointmentProcedureList.get(position).getProcedureID();
            ProcedureIDs.add(Integer.parseInt(holder.procedureId.getText().toString()));
        }

    }
    @Override
    public int getItemCount(){
        return labAppointmentProcedureList.size();
    }
}

0 个答案:

没有答案