0
votes

So I want to know if there is a way to query based on inner child without having to know the parent in Firebase Database? Here is the exact situation: As shown in the image, I want to query the entire data snapshot where title = "bj", since I have pushed them automatically so Im unaware of the Key (or parent in this case). Im curious if Fb Query has anyway to look into the nested childs directly? TIAenter 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

1 Answers

1
votes

So I found the answer from another post on StackOverFlow that suggested the solution should be something like:

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) {

            }
        });