how do i retrieve the value of all checkBox in the RecyclerView?

49 views Asked by At

The data succesfully added to the firebase, however it only send 3 data of recyclerView.

based on what chatGpt since only 3 recyclerView fit in screen,thats why only 3 succesfully retrieve

here is the code

public void submitButtonFunction(){
    submitButton = findViewById(R.id.submitButton);
    submitButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(date!=null) {
                DatabaseReference dateRef = rootRef.getReference()
                        .child("Subject")
                        .child(courseCode)
                        .child("attendance")
                        .child(date);
                List<String> selectedCheckBoxes = new ArrayList<>();
                boolean anyCheckBoxChecked = false;
                Log.d("Date", date);
                for (int i = 0; i < recyclerView.getChildCount(); i++) {
                    View view = recyclerView.getChildAt(i);
                    CheckBox attendCheckBox = view.findViewById(R.id.attendCheckBox);
                    CheckBox absentCheckBox = view.findViewById(R.id.absentCheckBox);
                    CheckBox excuseCheckBox = view.findViewById(R.id.excuseCheckBox);

                    if (attendCheckBox.isChecked()) {
                        selectedCheckBoxes.add("Attend");
                        anyCheckBoxChecked = true;
                    } else if (absentCheckBox.isChecked()) {
                        selectedCheckBoxes.add("Absent");
                        anyCheckBoxChecked = true;
                    } else if (excuseCheckBox.isChecked()) {
                        selectedCheckBoxes.add("Excuse");
                        anyCheckBoxChecked = true;
                    } else {
                        Toast.makeText(studentAttendance.this, "Please Check All the checkboxes", Toast.LENGTH_SHORT).show();
                        selectedCheckBoxes.add("Not Checked");
                    }
                    String tempNameMatric = list.get(i).getStudentMatricNo() + " " + list.get(i).getStudentName() ;
                    dateRef.child(tempNameMatric).setValue(selectedCheckBoxes.get(i))
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    Toast.makeText(studentAttendance.this, "Succesful submit attendance", Toast.LENGTH_SHORT).show();

                                }
                            });

                }
            }else{
                Toast.makeText(studentAttendance.this, "Please Enter Date Of Attendance", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

i have tried -change the loop condition i < list.size() -Based on chatGpt

for (int i = 0; i < list.size(); i++) {
    subject currentSubject = list.get(i);
    CheckBox attendCheckBox = recyclerView.findViewHolderForAdapterPosition(i)
                                .itemView.findViewById(R.id.attendCheckBox);
    CheckBox absentCheckBox = recyclerView.findViewHolderForAdapterPosition(i)
                                .itemView.findViewById(R.id.absentCheckBox);
    CheckBox excuseCheckBox = recyclerView.findViewHolderForAdapterPosition(i)
                                .itemView.findViewById(R.id.excuseCheckBox);
1

There are 1 answers

5
Rainy sidewalks On

try insted of

 for (int i = 0; i < recyclerView.getChildCount(); i++) {
// all the logic was here
}

the above will only count and returns the number of child views currently attached to the RecyclerView ,which is pretty much you have on screen .

use

for (int i = 0; i < recyclerView.getAdapter().getItemCount(); i++) {
    // all of the code remains in here as you have
}

read more on it recyclerview-getchildcount-is-returning-different-number-of-child-everytime

EDIT:

for

null pointer Process: com.example.collegesystem, PID: 13229 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

1st just wrap the code in if(date!=null)/else to ensure not to null view is passed further.

and i place of recyclerView.getChildAt(i); try with recyclerView.findViewHolderForAdapterPosition(i)

public void submitButtonFunction(){
  submitButton = findViewById(R.id.submitButton);
  submitButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
          if(date!=null) {
              DatabaseReference dateRef = rootRef.getReference()
                      .child("Subject")
                      .child(courseCode)
                      .child("attendance")
                      .child(date);
              List<String> selectedCheckBoxes = new ArrayList<>();
              boolean anyCheckBoxChecked = false;
              Log.d("Date", date);
              for (int i = 0; i <  recyclerView.getAdapter().getItemCount(); i++) {
                  View view =recyclerView.findViewHolderForAdapterPosition(i)//recyclerView.layoutManager?.findViewByPosition(i) // recyclerView.getChildAt(i);
                  if (view != null) {
                  CheckBox attendCheckBox = view.findViewById(R.id.attendCheckBox);
                  CheckBox absentCheckBox = view.findViewById(R.id.absentCheckBox);
                  CheckBox excuseCheckBox = view.findViewById(R.id.excuseCheckBox);

                  if (attendCheckBox.isChecked()) {
                      selectedCheckBoxes.add("Attend");
                      anyCheckBoxChecked = true;
                  } else if (absentCheckBox.isChecked()) {
                      selectedCheckBoxes.add("Absent");
                      anyCheckBoxChecked = true;
                  } else if (excuseCheckBox.isChecked()) {
                      selectedCheckBoxes.add("Excuse");
                      anyCheckBoxChecked = true;
                  } else {
                      Toast.makeText(studentAttendance.this, "Please Check All the checkboxes", Toast.LENGTH_SHORT).show();
                      selectedCheckBoxes.add("Not Checked");
                  }
                  String tempNameMatric = list.get(i).getStudentMatricNo() + " " + list.get(i).getStudentName() ;
                  dateRef.child(tempNameMatric).setValue(selectedCheckBoxes.get(i))
                          .addOnCompleteListener(new OnCompleteListener<Void>() {
                              @Override
                              public void onComplete(@NonNull Task<Void> task) {
                                  Toast.makeText(studentAttendance.this, "Succesful submit attendance", Toast.LENGTH_SHORT).show();

                              }
                          });

              }else{
                Toast.makeText(studentAttendance.this, "Please Check view passed it could be null recyclerView.getChildAt(i) at i="+i, Toast.LENGTH_LONG).show();
              }}
          }else{
              Toast.makeText(studentAttendance.this, "Please Enter Date Of Attendance", Toast.LENGTH_SHORT).show();
          }
      }
  });