1

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.

enter image description here

enter image description here

enter image description here

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) {

}
}
  • 2
    Check **[this](https://stackoverflow.com/questions/53378868/firestore-firebase-android-search-and-update-query)** and **[this](https://stackoverflow.com/questions/52179367/how-to-update-one-field-from-all-documents-using-pojo-in-firestore)** out. – Alex Mamo May 06 '19 at 12:32
  • @AlexMamo Hi, the second link seems to be a potential solution but I'm not sure with how to proceed for my own project, could you help out with the coding aspect for the update method? –  May 06 '19 at 12:44
  • I cannot code for you. You should make your own attempt given the information in the linked answers and ask another question if something else comes up. – Alex Mamo May 06 '19 at 12:57
  • 1
    @AlexMamo It turns out the solution you shared only updates a single field with the same value for all the objects. However I want to update multiple objects with multiple values. So this question is not a duplicate –  May 06 '19 at 16:11
  • In that case you should update the properties under each object with the desired values. Even so, it's still a duplicate. – Alex Mamo May 06 '19 at 16:14
  • @AlexMamo How do I update "under each object"? –  May 06 '19 at 16:15
  • Get a reference to that object and update the properties beneth it. – Alex Mamo May 06 '19 at 16:16
  • @AlexMamo How would that work for a large number of objects? Basically there's a list of these objects/their values being displayed in the app with a text field to edit the values for each object. –  May 06 '19 at 16:17
  • In the exact way it is explained in the duplicate. So yu should make your own attempt given the information in the duplicate and ask another question if something else comes up. – Alex Mamo May 06 '19 at 16:27
  • @AlexMamo In the array, we have maps containing an array containing maps and then another array of maps. How do represent this path in such a way that we can update the fields inside the last level of maps in the final array? –  May 07 '19 at 10:23
  • Get the reference of that last object and update it accordingly. – Alex Mamo May 07 '19 at 10:33
  • @AlexMamo We've tried but didn't succeed as we lack sufficient knowledge. How do we do that? –  May 07 '19 at 10:33

0 Answers0