0

Previously, I asked to get the data in firebase. But I can't get the value... My database like this.

{Singer :
    {Eminem :
        {01 :
            songName : "Rap god",
            songType : "type01"
        },
        {02 :
            songName : "Till I collapse",
            songType : "type02"
        }
    },
    {Lauv :
        {01 :
            songName : "I Like Me Better",
            songType : "type01"
        },
        {02 :
            songName : "lonely",
            songType : "type03"
        }
    },
    {Alan Walker :
        {01 : 
            songName : "All Falls Down",
            songType : "type02"
        },
        {02 :
            songName : "On My Way",
            songType : "type01"
        }
    }  
}            

I want to get all type01 in Singer all of them. I tried code like this

@Override
public void onBindViewHolder(@NonNull MyHolder holder, int i) {
    ...
    Query myQuery = FirebaseDatabase.getInstance().getReference().child("Singer").orderByChlid("songType").equalsTo("type01");
    FirebaseRecyclerOption.Builder<ItemModel>.setQuery(myQuery, new SanpshotParser<ItemModel>() {
        @NonNull
        @Override
        public ItemModel parseSnapshot(@NonNull DataSnapshot snapshot) {
            String songStr = snapshot.child("songName").getValue().toString();
            String typeStr = snapshot.child("songType").getValue().toString();

            return new ItemModel(songStr, typeStr);
        }
    }).build();
    ...
}

But I can't get what I want. FirebaseDatabase.getInstance().getReference().child("Singer").orderByChild("songType").equalsTo("type01") can't read the data in function parseSnapshot. How can I get all the singer's songs that type is type01?

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
H.JiMan
  • 269
  • 1
  • 2
  • 10
  • your trying to retrieve the child of child from above query. So it won't work as you expected. Even frank van puff explained in his [answer](https://stackoverflow.com/a/43295289/10182897) – Ashish Sep 25 '19 at 03:55
  • 1
    In my previous answer I gave you the two options: 1) build your own adapter, 2) change your data structure to a flat list of songs, so FirebaseUI can work with it. You seem to be doing neither here, which is the reason it won't work. – Frank van Puffelen Sep 25 '19 at 05:05

1 Answers1

2

You need to go one step deeper so the query can access songType:

FirebaseDatabase.getInstance().getReference().child("Singer").child("Eminem").orderByChild("songType").equalsTo("type01")
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • I suggest to put recyclerview to retrieve the list of singer. Then get onClick of each singer name retrieve the type01 song – Ashish Sep 25 '19 at 04:00
  • Me too good idea – Peter Haddad Sep 25 '19 at 04:02
  • There is only/at most one song with `type01` per singer it seems. So it could make sense to show *that* for each singer. It's just that the data structure OP uses isn't suitable for doing that FirebaseUI to do that, which is why I recommended a flat data structure in answer to their previous question. – Frank van Puffelen Sep 25 '19 at 05:07