基本上,这是我在Firestore中的数据模型: 集合>文档>包含地图数组>包含数组的地图>包含地图的数组
我能够读取和显示这些地图及其值,并且我学会了如何更新单个地图的值,但不能更新如下所示的地图数组:https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects
应用逻辑是这样的: 用户将获得程序(文档)列表,单击将要求输入学期,输入学期后,将加载该特定学期的论文列表,现在用户将选择特定论文,然后加载该论文。 TotalDays 和该论文中的学生列表及其 ID 和 DaysPresent 。
但是,我正试图找到一种方法来更新多个对象(又称地图)的字段。具体来说,我正在尝试为每个纸质地图和每个学生地图的 DaysPresent 更新 DaysTotal 。
我使用RecyclerView来显示数据。
纸张模型类:
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;
}
}
学生模型类:
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;
}
}
我显示DaysTotal,ID和出现的天数并需要实现更新方法的代码:
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) {
}
}