我在Firebase中有两个节点,business
和science
,这两个节点都位于名为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
型号
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;
}
}
代码我正在尝试从两个节点获取数据
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);
}
}
使用上面的代码似乎会返回带有空数据的ArrayLists
。
我的目标是检索数据并存储在不同的ArrayList
,businessList
和scienceList
中。 ArrayLists
都将用于比较数据。
我的问题是检索到的数据不会在ArrayLists
中保留。如何确保检索到的数据存储在ArrayLists
中,以便我以后可以比较数据?
答案 0 :(得分:1)
您没有将侦听器附加到正确的引用。
将其更改为
businessRef.addListenerForSingleValueEvent(new ValueEventListener() {
scienceRef.addListenerForSingleValueEvent(new ValueEventListener() {
修改强>
在迭代中,您没有从条目中调用getValue。 改为这个
for (DataSnapshot ds : dataSnapshot.getChildren()) {
CourseDetails c = ds.getValue(CourseDetails.class);
您希望确保仅在两个侦听器都调用onDataChange时才调用比较方法,您可以use my solution in another similar question或use a utility class from an answer from a firebaser here。