2016-08-13 41 views
0

我在火力地堡,businessscience兩個節點,都在一個叫做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,businessListscienceList。這兩個ArrayLists將用於比較數據。

我的問題是檢索的數據不會持續在ArrayLists。如何確保檢索到的數據存儲在ArrayLists中,以便以後可以比較數據?

回答

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 questionuse a utility class from an answer from a firebaser here

+0

再次威利克。真的很抱歉,我忘了更新那部分。這兩個'Listeners'實際上都被正確地引用了。我更新了我的代碼 –

+0

啊好吧,你可以發佈你的數據庫結構和'CourseDetails'類嗎? @LorekBryanson – Wilik

+0

好的。用結構編輯我的問題 –