我正在尝试从出勤集合和学生集合中检索其userUID匹配的每个学生的姓名,即,如果两个用户UID匹配,我想从学生集合中返回与userUID关联的名称。我正在查询出勤表。
这是我的代码:
public void viewAttendance(View v) {
attendanceRef2.orderBy("sessionID", Query.Direction.ASCENDING)
.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
String data = "";
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Attendance attendance = documentSnapshot.toObject(Attendance.class);
Student studentName = documentSnapshot.toObject(Student.class);
String sessionID = attendance.getSessionID();
String studentID = attendance.getUserUID();
String attendanceUID = studentName.getUserUID();
String name;
if (studentID.equals(attendanceUID)){
name = studentName.getName();
data+= "Session ID: " + sessionID + "\n" + "Student Name: " + name + "\n\n";
}
}
textViewData.setText(data);
}
});
}
修改后的代码
public void viewAttendance(View v) {
attendanceRef2.orderBy("sessionID", Query.Direction.ASCENDING)
.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
String data = "";
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Attendance attendance = documentSnapshot.toObject(Attendance.class);
Student studentName = documentSnapshot.toObject(Student.class);
String sessionID = attendance.getSessionID();
String attendanceUID = attendance.getUserUID();
String studentUID = studentName.getUserUID();
final String name = "";
if (attendanceUID.equals(studentUID)){
db.collection("Student").document("name").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
textViewData.setText(name);
}
});
}
data+= "Session ID: " + sessionID + "\n" + "Student Name: " + name + "\n\n";
}
textViewData.setText(data);
}
});
}
答案 0 :(得分:0)
您越来越近了。您需要加载两个文档:
由于用户名来自第二个文档,因此只能在加载第二个文档后调用DocumentSnapshot.toObject
(因此,在第二个侦听器的onComplete
或onSuccess
回调中) 。像这样:
public void viewAttendance(View v) {
attendanceRef2.orderBy("sessionID", Query.Direction.ASCENDING)
.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
String data = "";
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Attendance attendance = documentSnapshot.toObject(Attendance.class);
String sessionID = attendance.getSessionID();
String attendanceUID = attendance.getUserUID();
String studentUID = studentName.getUserUID();
if (attendanceUID.equals(studentUID)){
db.collection("Student").document(studentUID).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
Student studentName = task.getResult().toObject(Student.class);
textViewData.setText(studentName);
// Or maybe: textViewData.setText(studentName.getName());
data+= "Session ID: " + sessionID + "\n" + "Student Name: " + studentName + "\n\n";
textViewData.setText(data);
}
});
}
}
}
});
}