如何对多个孩子执行查询?

我正在尝试进行一项显示课程并将其列在回收站视图中的活动。但是,我在从 firebase 查询数据时遇到问题。在我展示我的数据库结构后,我会解释更多。这是我的数据库结构:


"Classes": {

    "-Li9nBUgnFmCWqUmwV5W": {

        "class_info": {

            "date_clasname": "August 6, 2019",

            "room_number": "131",

            "subject": "Science",

            "teacher": "Ms.Henry",

            "id": "-Li9nBUgnFmCWqUmwV5W"

        }

    },

    "-Li9n_IYwbTNPdNAsTAu": {

        "class_info": {

            "date_clasname": "August 6, 2019",

            "room_number": "131",

            "subject": "Math",

            "teacher": "Ms.Henry",

            "id": "-Li9n_IYwbTNPdNAsTAu"

        }

    },

    "-Li9naUipsa865NBpZjW": {

        "class_info": {

            "date_clasname": "August 6, 2019",

            "room_number": "131",

            "subject": "Other",

            "teacher": "Ms.Henry",

            "id": "-Li9naUipsa865NBpZjW"

        }

    },

    "-Li9nbpwp2qchwmZRDP9": {

        "class_info": {

            "date_clasname": "August 6, 2019",

            "room_number": "131",

            "subject": "Technology",

            "teacher": "Ms.Henry",

            "id": "-Li9nbpwp2qchwmZRDP9"

        }

    },

    "-Li9ne0NDyjcB4SIWi1z": {

        "class_info": {

            "date_clasname": "August 6, 2019",

            "room_number": "131",

            "subject": "Social Studies",

            "teacher": "Ms.Henry",

            "id": "-Li9ne0NDyjcB4SIWi1z"

        }

    }

},

我想我的问题可能是我无法获取随机生成的 ID 并将其设置为我的孩子,然后再在我的 onChildAdded 中将其设置为我的另一个孩子“class_info”。谢谢你!



青春有我
浏览 86回答 1
1回答

慕的地8271018

您正在构建这样的查询:Query query = myRef.child(id).child("class_info").orderByChild("subject").equalTo("Science");这表示要通过id(我认为您的 JSON 中未显示)找到一个孩子,然后找到一个孩子class_info,然后在其下找到subject每个孩子的属性并对其进行过滤。所以路径是$id/class_info/*/subject,其中*是你要过滤返回的子节点。但你实际拥有的是/Classes/*/class_info/subject. 所以你有一个节点Classes,你想返回其class_info/subject具有给定值的子节点。在代码中这是:DatabaseReference classesRef = FirebaseDatabase.getInstance().getReference("Classes");Query query = classesRef.orderByChild("class_info/subject").equalTo("Science");query.addListenerForSingleValueEvent(new ValueEventListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onDataChange(@NonNull DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; list = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class_model new_class = dataSnapshot1.getValue(Class_model.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java