0
我在火力地堡,business
和science
兩個節點,都在一個叫做courses
的Android - 檢索來自不同節點的數據在火力地堡
{
"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
中,以便以後可以比較數據?
再次威利克。真的很抱歉,我忘了更新那部分。這兩個'Listeners'實際上都被正確地引用了。我更新了我的代碼 –
啊好吧,你可以發佈你的數據庫結構和'CourseDetails'類嗎? @LorekBryanson – Wilik
好的。用結構編輯我的問題 –