簡體   English   中英

如何在 firebase android 中獲取子數據的子數據

[英]how to get child of child of child data in firebase android

firebase 數據庫圖片

我怎樣才能得到這些數據?

我在鍵值方面遇到問題,因為這是推鍵,這是我首先添加的一些保存數據的代碼。

創建表

reference = FirebaseDatabase.getInstance().getReference("COURSE");
String courseId = reference.push().getKey();
if (courseId != null) {
    CourseTObject object = new CourseTObject();
    object.setcId(courseId);
    object.settId(firebaseUser.getUid());
    object.setInstitute(instituteName);
    object.setCourse(courseName);
    if (section.equals("")){
        object.setSection(null);
    }else {
        object.setSection(section);
    }
    reference.child(courseId).setValue(object);
}

添加一些數據

reference = reference.child(nodeId).child(quizTitleString);
reference.child("totalQuestions").setValue(questions);
reference.child("timeLimit").setValue(timer);
reference.child("eachQuestionMarks").setValue(marks);
reference.child("quizTitle").setValue(quizTitleString);

然后在下面提到一些子數據

reference = FirebaseDatabase.getInstance().getReference("COURSE");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
            CourseTObject courseObject = dataSnapshot.getValue(CourseTObject.class);
            assert courseObject != null;
            if (courseObject.gettId().equals(firebaseUser.getUid())) {
                if (courseObject.getInstitute().equals(institute)){
                    if (courseObject.getCourse().equals(course)){
                        if (courseObject.getSection().equals(section)){
                            String nodeId = dataSnapshot.getKey();
                            assert nodeId != null;
                            reference = reference.child(nodeId).child(quizTitleString).child("QUESTION"+questionNo);
                            reference.child("question").setValue(q);
                            reference.child("choice1").setValue(c1);
                            reference.child("choice2").setValue(c2);
                            reference.child("choice3").setValue(c3);
                            reference.child("choice4").setValue(c4);
                            reference.child("answer"+answerNo).setValue(answer);
                        }
                    }
                }
            }
        }
    }

model class CourseTObject 只有一些 getter setter exclude child of child nodes data

public class CourseTObject {
    private String cId;
    private String tId;
    private String institute;
    private String course;
    private String section;
    private String totalQuestions;
    private String timeLimit;
    private String eachQuestionMarks;

    public CourseTObject() {
    }

    public CourseTObject(String cId, String tId, String institute, String course, String section, String totalQuestions, String timeLimit, String eachQuestionMarks) {
        this.cId = cId;
        this.tId = tId;
        this.institute = institute;
        this.course = course;
        this.section = section;
        this.totalQuestions = totalQuestions;
        this.timeLimit = timeLimit;
        this.eachQuestionMarks = eachQuestionMarks;
    }

    public String getcId() {
        return cId;
    }
    public void setcId(String cId) {
        this.cId = cId;
    }

    public String gettId() {
        return tId;
    }
    public void settId(String tId) {
        this.tId = tId;
    }

    public String getInstitute() {
        return institute;
    }
    public void setInstitute(String institute) {
        this.institute = institute;
    }

    public String getCourse() {
        return course;
    }
    public void setCourse(String course) {
        this.course = course;
    }

    public String getSection() {
        return section;
    }
    public void setSection(String section) {
        this.section = section;
    }

    public String getTotalQuestions() {
        return totalQuestions;
    }
    public void setTotalQuestions(String totalQuestions) {
        this.totalQuestions = totalQuestions;
    }

    public String getTimeLimit() {
        return timeLimit;
    }
    public void setTimeLimit(String timeLimit) {
        this.timeLimit = timeLimit;
    }

    public String getEachQuestionMarks() {
        return eachQuestionMarks;
    }
    public void setEachQuestionMarks(String eachQuestionMarks) {
        this.eachQuestionMarks = eachQuestionMarks;
    }    
}

如果我們看這段代碼:

reference = FirebaseDatabase.getInstance().getReference("COURSE");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
            CourseTObject courseObject = dataSnapshot.getValue(CourseTObject.class);
            ...

因此,您在數據庫中的COURSE處加載所有數據,然后遍歷直接子節點(因此-N9...在您的屏幕截圖中),然后嘗試從中讀取CourseTObject

但是,如果我們查看您的數據庫的屏幕截圖,則該直接子節點中沒有cIdtIdinstitude和類似屬性。 這些屬性在QUIZ_1下低一級。

事情應該會變得更好,你添加第二個循環:

reference = FirebaseDatabase.getInstance().getReference("COURSE");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot pushkeySnapshot : snapshot.getChildren()) {
           for (DataSnapshot quiz1Snapshot : snapshot.getChildren()) {
                CourseTObject courseObject = quiz1Snapshot.getValue(CourseTObject.class);
                ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM