Basically, this is my data model in Firestore: Collection>Document>Contains an array of maps>maps containing arrays>Arrays containing maps
I'm able to read and display these maps and their values and I have learnt how to update a single map's values but not for an array of maps as shown here: https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects
App logic is something like this: User will get a list of programmes (documents), when clicked will ask for the Semester, when the semester is entered, it will load the list of papers for that particular semester, now user will select specific paper, and then it will load the TotalDays and a list of the students inside that paper + their ID and DaysPresent.
However, I'm trying to find a way to update the fields of multiple objects (aka maps). Specifically, I'm trying to update DaysTotal for each paper map and each student map's DaysPresent.
I used RecyclerView for displaying the data.
Paper Model Class:
public final class Paper {
private String name;
private String courseCode;
private Long totalDays;
private List<Student> studentList;
public Paper(String name, String courseCode, Long totalDays) {
this.name = name;
this.courseCode = courseCode;
this.totalDays = totalDays;
}
public void setTotalDays(Long totalDays) {
this.totalDays = totalDays;
}
public void setStudentList(@NonNull List<Student> studentList) {
this.studentList = studentList;
}
public String getName() {
return name;
}
public String getCourseCode() {
return courseCode;
}
public Long getTotalDays() {
return totalDays;
}
public List<Student> getStudentList() {
return studentList;
}
}
Student Model Class:
public final class Student {
private String studentId;
private Long daysPresent = 0L;
public Student(String studentId, Long daysPresent) {
this.studentId = studentId;
this.daysPresent = daysPresent;
}
public void setDaysPresent(Long daysPresent) {
this.daysPresent = daysPresent;
}
public String getStudentId() {
return studentId;
}
public Long getDaysPresent() {
return daysPresent;
}
}
The Code where I displayed DaysTotal, ID and Days Present and need to implement an update method:
public class EditAttendance2 extends AppCompatActivity implements StudentAdapter.OnItemClickListener {
private static final String TAG = EditAttendance2.class.getSimpleName();
private RecyclerView recyclerView;
Long semesterValue;
Integer paperID;
public String Dept;
private EditText total;
private List<Paper> paperList;
//private List<Student> studentList;
private StudentAdapter studentAdapter;
private boolean activeDbCall;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
String documentID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_attendance2);
recyclerView = findViewById(R.id.student_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(EditAttendance2.this));
total = findViewById(R.id.totalEdit);
Bundle extras = getIntent().getExtras();
if (extras != null) {
paperID = extras.getInt(EditAttendance.PAPER_POS);
semesterValue = extras.getLong(EditAttendance.SEMESTER_ID);
documentID = extras.getString(EditAttendance.DOCUMENT_ID);
//Log.d(TAG, documentID);
}
Dept = PrefUtilities.with(this).getDepartment();
Display();
}
public void Display() {
if (!activeDbCall) {
activeDbCall = true;
final CollectionReference Department = db.collection(Dept);
final DocumentReference dbDocs = Department.document(documentID);
dbDocs.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
if (documentSnapshot != null) {
Map<String, Object> data = documentSnapshot.getData();
if (data != null) {
Semesters semesters = new Semesters(data);
paperList = semesters.getPaperList(semesterValue);
Paper paper =paperList.get(paperID);
Long totalDays = paper.getTotalDays();
Log.d(TAG,"****************************************************");
Log.d(TAG,"Total Days: " + totalDays);
Log.d(TAG,"****************************************************");
List<Student> studentList = paper.getStudentList();
total.setText(totalDays.toString());
studentAdapter = new StudentAdapter(studentList);
studentAdapter.setOnItemClickListener(EditAttendance2.this);
recyclerView.setAdapter(studentAdapter);
}
}
}
});
}
}
@Override
public void onItemClick(int position) {
}
@Override
public void onUpdateClick(int position) {
}
@Override
public void onDeleteClick(int position) {
}
}