0

Here is the preview of the tree:

enter image description here

I tried to do this:

mRef.orderByChild("desc").equalTo(des).addChildEventListener(new ChildEventListener()
{
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s)
    {
        mRef.orderByChild("userid").equalTo(uid).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s)
            {
                myParentNode = dataSnapshot.getKey();
            }
    });
});

But it returns the value of the previously visited node.

Note: The Parent node "ASSIST Blog" contains many children which may have similar values.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • mDatabase=FirebaseDatabase.getInstance(); mRef=mDatabase.getReference().child("AssistBlog"); – Abhishek Kumar Jun 14 '18 at 07:07
  • explain an example:what should the input be and the output? –  Jun 14 '18 at 07:12
  • consider the first node in AssistBlog i.e "-LEmmhJ8zYxWeGrtGWYy" it has desc as "Press the add icon on top to start adding posts to feed." and userid as "bd9arFKXCGRwDIqH7dkLjlW8CPk2" I want to get the parent node "-LEmmhJ8zYxWeGrtGWYy" using user id and desc – Abhishek Kumar Jun 14 '18 at 07:16

2 Answers2

0

You should call the getKey() method in your retrieved dataSnapshot:

final Query userQuery = mRef.orderByChild("First Name");

  userQuery.addChildEventListener(new ChildEventListener() {
     @Override
     public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        map.clear();
        //Get the node from the datasnapshot
        String myParentNode = dataSnapshot.getKey();
        for (DataSnapshot child: dataSnapshot.getChildren())
        {
           String key = child.getKey().toString();
           String value = child.getValue().toString();
           map.put(key,value);
        }
Mayur Patel
  • 2,300
  • 11
  • 30
0

You cannot achieve this with Firebase Realtime database without some changes to your database. Firebase Realtime database does not have the capability to perform filtering on multiple conditions. In SQL terms, cannot use "multiple where clauses". If you want to check for matches on multiple properties, you'll have to create a composite field that contains a combination of the things you're looking for. For example, you'll need a string value that is composed from your desc property concatenated with your userid property and for this please see my answer from this post.

If you are interested, Firestore allows you to filter on multiple conditions and for that I recommend you see the official documentation.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193