Home > Net >  Checkbox of an element in the listing changes the checkbox of other elements in ViewHolder
Checkbox of an element in the listing changes the checkbox of other elements in ViewHolder

Time:02-10

My appointment listing page lists all appointments made, and the cb_attendance component of one element is changing that of other elements.

Error example: There are three scheduling elements, I set the first element to true and when I exit and return to the page the other two elements have their checkboxes changed to true.

I don't want the other checkboxes to change together, how to fix this?

The component that requires attention is the cb_attendance.

  • Code
    public class ViewHolder extends RecyclerView.ViewHolder{
        EditText edt_id_scheduling, edt_date, edt_hour;
        AutoCompleteTextView act_area, act_service, act_Eprofessional, act_Eclient;
        CheckBox cb_attendance;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            edt_id_scheduling = itemView.findViewById(R.id.edt_id_scheduling);
            edt_date = itemView.findViewById(R.id.edt_date);
            edt_hour = itemView.findViewById(R.id.edt_hour);
            act_area = itemView.findViewById(R.id.act_area);
            act_Eprofessional = itemView.findViewById(R.id.act_Eprofessional);
            act_service = itemView.findViewById(R.id.act_service);
            act_Eclient = itemView.findViewById(R.id.act_Eclient);
            cb_attendance = itemView.findViewById(R.id.cb_attendance);
            cb_attendance.setChecked(UpdateFalse("MPS"));
            cb_attendance.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    UpdateTrue("MPS", b);
                }
            });
        }
    }
    private void UpdateTrue(String key, boolean value){
        SharedPreferences sharedPreferences = context.getSharedPreferences("MPS", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key,value);
        editor.apply();
    }
    private Boolean UpdateFalse(String key){
        SharedPreferences sharedPreferences = context.getSharedPreferences("MPS", Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key,false);
    }

CodePudding user response:

  •  Tags:  
  • Related