Trying to retrieve field from one collection based on UID of two collections matching












0















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 querying 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);
}
});
}









share|improve this question

























  • This is currently retrieving the session ID although returning null in the student name

    – Sean Gallagher
    Aug 22 '18 at 12:18











  • 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 '18 at 12:53











  • This is where I have added your suggestions. It retrieves the sessionID then disappears showning nothing?

    – Sean Gallagher
    Aug 22 '18 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 '18 at 13:31











  • so I would have to insert this in the for loop or just before beside the query

    – Sean Gallagher
    Aug 22 '18 at 13:35
















0















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 querying 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);
}
});
}









share|improve this question

























  • This is currently retrieving the session ID although returning null in the student name

    – Sean Gallagher
    Aug 22 '18 at 12:18











  • 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 '18 at 12:53











  • This is where I have added your suggestions. It retrieves the sessionID then disappears showning nothing?

    – Sean Gallagher
    Aug 22 '18 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 '18 at 13:31











  • so I would have to insert this in the for loop or just before beside the query

    – Sean Gallagher
    Aug 22 '18 at 13:35














0












0








0








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 querying 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);
}
});
}









share|improve this question
















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 querying 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);
}
});
}






android google-cloud-firestore






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 7:37









marc_s

573k12811071255




573k12811071255










asked Aug 22 '18 at 12:17









Sean GallagherSean Gallagher

406




406













  • This is currently retrieving the session ID although returning null in the student name

    – Sean Gallagher
    Aug 22 '18 at 12:18











  • 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 '18 at 12:53











  • This is where I have added your suggestions. It retrieves the sessionID then disappears showning nothing?

    – Sean Gallagher
    Aug 22 '18 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 '18 at 13:31











  • so I would have to insert this in the for loop or just before beside the query

    – Sean Gallagher
    Aug 22 '18 at 13:35



















  • This is currently retrieving the session ID although returning null in the student name

    – Sean Gallagher
    Aug 22 '18 at 12:18











  • 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 '18 at 12:53











  • This is where I have added your suggestions. It retrieves the sessionID then disappears showning nothing?

    – Sean Gallagher
    Aug 22 '18 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 '18 at 13:31











  • so I would have to insert this in the for loop or just before beside the query

    – Sean Gallagher
    Aug 22 '18 at 13:35

















This is currently retrieving the session ID although returning null in the student name

– Sean Gallagher
Aug 22 '18 at 12:18





This is currently retrieving the session ID although returning null in the student name

– Sean Gallagher
Aug 22 '18 at 12:18













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 '18 at 12:53





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 '18 at 12:53













This is where I have added your suggestions. It retrieves the sessionID then disappears showning nothing?

– Sean Gallagher
Aug 22 '18 at 13:00





This is where I have added your suggestions. It retrieves the sessionID then disappears showning nothing?

– Sean Gallagher
Aug 22 '18 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 '18 at 13:31





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 '18 at 13:31













so I would have to insert this in the for loop or just before beside the query

– Sean Gallagher
Aug 22 '18 at 13:35





so I would have to insert this in the for loop or just before beside the query

– Sean Gallagher
Aug 22 '18 at 13:35












1 Answer
1






active

oldest

votes


















0














You're getting closer. You need to load two documents:




  1. The attendance information

  2. The user information for the student


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:



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);
}
});

}
}
}
});
}





share|improve this answer


























  • 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 '18 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 '18 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 '18 at 14:01











  • it says attempt to invoke virtual method on a null object reference

    – Sean Gallagher
    Aug 22 '18 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 '18 at 14:59











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51966725%2ftrying-to-retrieve-field-from-one-collection-based-on-uid-of-two-collections-mat%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You're getting closer. You need to load two documents:




  1. The attendance information

  2. The user information for the student


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:



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);
}
});

}
}
}
});
}





share|improve this answer


























  • 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 '18 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 '18 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 '18 at 14:01











  • it says attempt to invoke virtual method on a null object reference

    – Sean Gallagher
    Aug 22 '18 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 '18 at 14:59
















0














You're getting closer. You need to load two documents:




  1. The attendance information

  2. The user information for the student


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:



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);
}
});

}
}
}
});
}





share|improve this answer


























  • 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 '18 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 '18 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 '18 at 14:01











  • it says attempt to invoke virtual method on a null object reference

    – Sean Gallagher
    Aug 22 '18 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 '18 at 14:59














0












0








0







You're getting closer. You need to load two documents:




  1. The attendance information

  2. The user information for the student


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:



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);
}
});

}
}
}
});
}





share|improve this answer















You're getting closer. You need to load two documents:




  1. The attendance information

  2. The user information for the student


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:



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);
}
});

}
}
}
});
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 22 '18 at 19:31

























answered Aug 22 '18 at 13:35









Frank van PuffelenFrank van Puffelen

231k28379403




231k28379403













  • 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 '18 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 '18 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 '18 at 14:01











  • it says attempt to invoke virtual method on a null object reference

    – Sean Gallagher
    Aug 22 '18 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 '18 at 14:59



















  • 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 '18 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 '18 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 '18 at 14:01











  • it says attempt to invoke virtual method on a null object reference

    – Sean Gallagher
    Aug 22 '18 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 '18 at 14:59

















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 '18 at 13:45





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 '18 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 '18 at 13:52





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 '18 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 '18 at 14:01





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 '18 at 14:01













it says attempt to invoke virtual method on a null object reference

– Sean Gallagher
Aug 22 '18 at 14:01





it says attempt to invoke virtual method on a null object reference

– Sean Gallagher
Aug 22 '18 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 '18 at 14:59





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 '18 at 14:59


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51966725%2ftrying-to-retrieve-field-from-one-collection-based-on-uid-of-two-collections-mat%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)