mardi 28 juillet 2020

Checkboxes not working correctly in RecyclerView

I am using a RecyclerView to display a list of activities saved in my Firebase database. I have added a checkbox to the layout of the activity view so that the user can select whether they want to add this activity to their audit profile.

When the user clicks the checkbox to select the activity, I want to add this activity to their profile (which is stored in a separate table) and update a field in the original activity table to say that it is now included in the audit profile. When they uncheck the box, it is removed from their profile and the original activity is updated to reflect this.

However, when I try doing this, my checkboxes start behaving incorrectly (such as a checkbox being checked that definitely hasn't been clicked by the user, checkboxes not staying ticked). This only happens when I have both the code to add the activity to the profile and to update the original activity.

Any ideas? I think it might be something to do with state, but I'm not sure how to go about doing this.

My code is as follows

 auditAdapter = new FirestoreRecyclerAdapter<Activity, AuditBuilder.AuditViewHolder>(auditBuilder) {
        @Override
        protected void onBindViewHolder(@NonNull final AuditBuilder.AuditViewHolder auditViewHolder, int i, @NonNull final Activity activity) {

            auditViewHolder.aAuditActivityName.setText(activity.getActivity_Name());
            auditViewHolder.aAuditActivityType.setText(activity.getActivity_Type());
            auditViewHolder.aAuditActivityDate.setText(activity.getActivity_Date());

            final String docId = auditAdapter.getSnapshots().getSnapshot(i).getId();

            auditViewHolder.auditCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){

                        //ADD DOCUMENT ID TO DATABASE
                        DocumentReference documentReference = fStore.collection("audits")
                                .document(user.getUid())
                                .collection("myAuditActivities")
                                .document(docId);


                        Map<String, Object> audit = new HashMap<>();

                        audit.put("Activity_ID", docId);
                        audit.put("Activity_Name", activity.getActivity_Name());
                        audit.put("Activity_Description", activity.getActivity_Description());
                        audit.put("Activity_Type", activity.getActivity_Type());
                        audit.put("Activity_Date", activity.getActivity_Date());
                        audit.put("Activity_Hours", activity.getActivity_Hours());
                        audit.put("Activity_Mins", activity.getActivity_Mins());
                        audit.put("Activity_Ref1", activity.getActivity_Ref1());
                        audit.put("Activity_Ref2", activity.getActivity_Ref2());
                        audit.put("Activity_Ref3", activity.getActivity_Ref3());
                        audit.put("Activity_Ref4", activity.getActivity_Ref4());
                        audit.put("Image_URL", activity.getImage_URL());
                        audit.put("In_Audit", true);

                        documentReference.set(audit).addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Log.d("TAG", "Activity successfully added to audit");
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(AuditBuilder.this, "Error, try again", Toast.LENGTH_SHORT).show();
                            }
                        });


                        //UPDATE BOOLEAN IN_AUDIT IN CPD ACTIVITIES LOCATION IN DATABASE TO TRUE
                        DocumentReference updateActivity = fStore.collection("cpdActivities")
                                .document(user.getUid())
                                .collection("myCPD")
                                .document(docId);

                        Map<String, Object> updateBoolean = new HashMap<>();
                        updateBoolean.put("In_Audit", true);

                        updateActivity.update(updateBoolean).addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Log.d("TAG", "In_Audit successfully updated");
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Log.d("TAG", "In_Audit update failed");
                            }
                        });

                    } else {
                        //CHECKBOX UNCHECKED, DELETES FROM AUDIT TABLE
                        DocumentReference docRef = fStore.collection("audits")
                                .document(user.getUid())
                                .collection("myAuditActivities")
                                .document(docId);
                        docRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Log.d("TAG", "Activity successfully removed from audit");
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(AuditBuilder.this, "Error, try again", Toast.LENGTH_SHORT).show();
                            }
                        });


                        //UPDATE BOOLEAN IN_AUDIT IN CPD ACTIVITIES LOCATION IN DATABASE BACK TO FALSE
                        DocumentReference updateActivity = fStore.collection("cpdActivities")
                                .document(user.getUid())
                                .collection("myCPD")
                                .document(docId);

                        Map<String, Object> updateBoolean = new HashMap<>();
                        updateBoolean.put("In_Audit", false);

                        updateActivity.update(updateBoolean).addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Log.d("TAG", "In_Audit successfully updated");
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Log.d("TAG", "In_Audit update failed");
                            }
                        });


                    }
                }
            });


            auditViewHolder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), ActivityDetails.class);
                    //DISPLAYS ALL THE ATTRIBUTES OF THE ACTIVITY
                    i.putExtra("Activity_Name", activity.getActivity_Name());
                    i.putExtra("Activity_Description", activity.getActivity_Description());
                    i.putExtra("Activity_Type", activity.getActivity_Type());
                    i.putExtra("Activity_Date", activity.getActivity_Date());
                    i.putExtra("Activity_Hours", activity.getActivity_Hours());
                    i.putExtra("Activity_Mins", activity.getActivity_Mins());
                    i.putExtra("Activity_Ref1", activity.getActivity_Ref1());
                    i.putExtra("Activity_Ref2", activity.getActivity_Ref2());
                    i.putExtra("Activity_Ref3", activity.getActivity_Ref3());
                    i.putExtra("Activity_Ref4", activity.getActivity_Ref4());
                    i.putExtra("Image_URL", activity.getImage_URL());
                    i.putExtra("Activity_ID", docId);
                    v.getContext().startActivity(i);
                }
            });



        }

        @NonNull
        @Override
        public AuditBuilder.AuditViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.auditbuilder_view_layout, parent,false);
            return new AuditViewHolder(view);
        }
    };



Aucun commentaire:

Enregistrer un commentaire