Firebase:在不知道父级的情况下查询内部子级

时间:2019-05-05 14:47:12

标签: android firebase firebase-realtime-database

所以我想知道是否有一种方法可以基于内部子查询而不必了解Firebase数据库中的父级?这是确切的情况:  如图所示,我想查询title =“ bj”的整个数据快照,因为我已经自动推送了它们,因此我不知道Key(在这种情况下是父级)。我想知道Fb Query是否可以直接查看嵌套子项吗? TIA enter image description here

private void fetchPinnedLocationsForSearchbar(String key) {

        Query query = MyApplication.database.getReference("/pins/").child("title").orderByKey().equalTo(key);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Log.e("-->X-DATA-FROM-QUERY", dataSnapshot.toString());
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    final BusinessModel pinItModel = new BusinessModel();
                    if (((Boolean) dataSnapshot1.child("ifBusiness").getValue())) {
                        if  
                            BusinessModel pinItModelTest = dataSnapshot1.getValue(BusinessModel.class);

                            Log.e("->PicCount", pinItModelTest.getPicCount() + "");
//
                            _data.add(pinItModelTest);


                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

1 个答案:

答案 0 :(得分:1)

因此,我从StackOverFlow上的另一篇文章中找到了答案,该答案表明解决方案应类似于:

FirebaseDatabase.getInstance().getReference("pins").orderByChild("title").equalTo("bj").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                Log.e("-->X--ChildAddedQUERY", dataSnapshot.toString()+"---"+s);

            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });