Trying to retrieve field from one collection based on UID of two collections matching
Trying to retrieve field from one collection based on UID of two collections matching
I am trying to retrieve the name of each student where their userUID matches up from the attendance collection and the student collection i.e if the two user UID's match I would like to return the name associated with the userUID from the student collection. I am quering the attendance table to begin.
Here is my code:
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 + "nn";
textViewData.setText(data);
);
EDITED CODE
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 + "nn";
textViewData.setText(data);
);
You're loading a single document from a single collection in the code your now have. If you want to load the student from a different collection, you will have to do so inside the loop with another
get()
like: db.collection("Users").document(studentID).get().addOnCompleteListener(...
.– Frank van Puffelen
Aug 22 at 12:53
get()
db.collection("Users").document(studentID).get().addOnCompleteListener(...
This is where I have added your suggestions. It retrieves the sessionID then disappears showning nothing?
– Sean Gallagher
Aug 22 at 13:00
You will need an
onSuccess
, just you like have for your outer listener. And then inside that new onSuccess
you load the user information from the new document that you loaded with Student studentName = documentSnapshot.toObject(Student.class);
– Frank van Puffelen
Aug 22 at 13:31
onSuccess
onSuccess
Student studentName = documentSnapshot.toObject(Student.class);
so I would have to insert this in the for loop or just before beside the query
– Sean Gallagher
Aug 22 at 13:35
1 Answer
1
You're getting closer. You need to load two documents:
Since the user name comes from the second document, you can only call DocumentSnapshot.toObject
once that second document has been loaded (so in the onComplete
or onSuccess
callback of the second listener). Something like:
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 + "nn";
textViewData.setText(data);
);
);
The name variable when trying to print out the data is confusing me Frank, after the if statement finishes how does it fetch the name based on the userUID's.
– Sean Gallagher
Aug 22 at 13:45
Aha... I has missed those lines. Since they need the name (which is only available after the second document has also loaded), they need to be inside the second callback. I updated my answer.
– Frank van Puffelen
Aug 22 at 13:52
with this updated solution I am getting a runtime error in the logcat, it is pointing to this line textViewData.setText(studentName);
– Sean Gallagher
Aug 22 at 14:01
it says attempt to invoke virtual method on a null object reference
– Sean Gallagher
Aug 22 at 14:01
when I search the student collection then use .document("name") is this implying that name is a document. Because I am looking for the name of the students which is a field within a document held in the student collection
– Sean Gallagher
Aug 22 at 14:59
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This is currently retrieving the session ID although returning null in the student name
– Sean Gallagher
Aug 22 at 12:18