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);
try insted of
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
read more on it recyclerview-getchildcount-is-returning-different-number-of-child-everytime
EDIT:
for
1st just wrap the code in
if(date!=null)/elseto ensure not to null view is passed further.and i place of
recyclerView.getChildAt(i);try withrecyclerView.findViewHolderForAdapterPosition(i)