Reputation: 195
I have two nodes in Firebase, business
and science
, both under a same parent called courses
{
"courses" : {
"business" : {
"-KP-EadFq_rsXymxbUas" : {
"courseCHours" : "4",
"courseCode" : "BIZ2000",
"courseName" : "Business Mathematics",
"courseYear" : "1",
"prerequisite" : {
"-KP-EcGRXCtlXYQvjoYo" : {
"preMainCourse" : "Business Mathematics",
"preSubCourse" : "null"
}
},
"sections" : {
"-KP-EbIkS1jxVfNIE5jH" : {
"sectionCode" : "A1",
"sectionSeats" : "20"
},
"-KP-EbklnKpsUIIz_U68" : {
"sectionCode" : "A2",
"sectionSeats" : "20"
}
}
}
},
"science" : {
"-KP-EKJeu5PdfyJZnIVk" : {
"courseCHours" : "4",
"courseCode" : "SCI3000",
"courseName" : "Science Fundamentals",
"courseYear" : "1",
"prerequisite" : {
"-KP-EOVlKZx4zofkg5RT" : {
"preMainCourse" : "Science Fundamentals",
"preSubCourse" : "null"
}
},
"sections" : {
"-KP-EM8vYB9d0-axX8Cr" : {
"sectionCode" : "A1",
"sectionSeats" : "20"
},
"-KP-EModVxv_Vc3SBZ_G" : {
"sectionCode" : "A2",
"sectionSeats" : "20"
}
}
},
"-KP-EVvdvHgqxGPsxCrE" : {
"courseCHours" : "4",
"courseCode" : "SCI3100",
"courseName" : "Science History",
"courseYear" : "1",
"prerequisite" : {
"-KP-EXa8naK5uF7y9py2" : {
"preMainCourse" : "Science History",
"preSubCourse" : "null"
}
},
"sections" : {
"-KP-EWX0hh8q5d5YTkVR" : {
"sectionCode" : "A1",
"sectionSeats" : "20"
},
"-KP-EWu8pWXS1aR9zh_4" : {
"sectionCode" : "A2",
"sectionSeats" : "20"
}
}
}
}
}
}
CourseDetails
model
public class CourseDetails {
private String courseCode;
private String courseName;
private String courseCHours;
private String courseYear;
private String courseKey;
public CourseDetails() {
}
public CourseDetails(String courseCode, String courseName, String courseCHours, String courseYear) {
this.courseCode = courseCode;
this.courseName = courseName;
this.courseCHours = courseCHours;
this.courseYear = courseYear;
}
@Exclude
public String getCourseKey() {
return courseKey;
}
public void setCourseKey(String courseKey) {
this.courseKey = courseKey;
}
public String getCourseCode() {
return courseCode;
}
public void setCourseCode(String courseCode) {
this.courseCode = courseCode;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseCHours() {
return courseCHours;
}
public void setCourseCHours(String courseCHours) {
this.courseCHours = courseCHours;
}
public String getCourseYear() {
return courseYear;
}
public void setCourseYear(String courseYear) {
this.courseYear = courseYear;
}
}
Code I'm trying to get data from both nodes
ArrayList<String> businessList;
ArrayList<String> scienceList;
DatabaseReference businessRef = FirebaseDatabase.getInstance().getReference().child("courses").child("business");
businessRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
displayBusiness(dataSnapshot);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference scienceRef = FirebaseDatabase.getInstance().getReference().child("courses").child("science");
scienceRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
displayScience(dataSnapshot);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
public void displayBusiness(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
CourseDetails c = ds.getValue(CourseDetails.class);
String code = c.getCourseCode();
String name = c.getCourseName();
String CodeName = code + " " + name;
businessList.add(CodeName);
}
}
public void displayScience(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
CourseDetails c = ds.getValue(CourseDetails.class);
String code = c.getCourseCode();
String name = c.getCourseName();
String CodeName = code + " " + name;
scienceList.add(CodeName);
}
}
Using the code above seems like returns the ArrayLists
with empty data.
My goal is to retrieve the data and store in different ArrayList
, businessList
and scienceList
. Both the ArrayLists
will be used for comparing the data.
My problem is the retrieved data does not persist in the ArrayLists
. How to make sure that the retrieved data is stored in the ArrayLists
so that I can compare the data later on?
Upvotes: 0
Views: 1007
Reputation: 7720
You are not attaching the listener to the right reference.
Change it to these
businessRef.addListenerForSingleValueEvent(new ValueEventListener() {
scienceRef.addListenerForSingleValueEvent(new ValueEventListener() {
EDIT
Inside the iteration, you're not calling the getValue from the entry. Change to this
for (DataSnapshot ds : dataSnapshot.getChildren()) {
CourseDetails c = ds.getValue(CourseDetails.class);
You want to make sure that the comparison method is called only when both of the listeners have called onDataChange, You can use my solution in another similar question or use a utility class from an answer from a firebaser here.
Upvotes: 1