Omar Skouri
Omar Skouri

Reputation: 33

Retrieving data from FireBase Database containing multiple subnodes using a specific orderby

i have been trying to retrieve the Data "in red" without knowing "the blue" key's value ( assuming there is multiple child with random keys) ,but i couldn't come up with the appropriate code . This is the Firebase Children tree.

db image

I tried to write this code but it doesn't work (i am a beginner)

 List<String> idlist = new ArrayList<String>();
 DatabaseStage = FirebaseDatabase.getInstance().getReference("Stages");
 Query query = DatabaseStage.orderByChild("uniqueId").equalTo(stageid);
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Iterable<DataSnapshot> stagedata = dataSnapshot.child("userEtudList").getChildren();
            for(DataSnapshot it: stagedata){
                if (!(it.getValue().equals("test"))){
                    idlist.add(it.getValue().toString());
                }
            }

Ps : if you are wondering about the use of orderByChild(uniqueId).equalTo(stageid) in this case stageid is equal to 2fdedf71-4346-4395-bee5-38db04d4fd47 it allows me to select one specific child which is the second one .

Here is the state of the variables obtained using breakpoints as you can see the Datasnapshot it keeps getting {key = userEtudList , Value =null} but not getting to Children

image

Upvotes: 2

Views: 96

Answers (3)

robsiemb
robsiemb

Reputation: 6374

For the purposes of this answer I'm interpreting your question as "I want to generate a list of all values, without regard for their key, in the userEtudList node of the selected Stages node. (e.g. tell me which students are in that course). Likewise, that your code that deals with the test values is really just test code and isn't really relevant to this challenge.


Your problem is that dataSnapshot isn't really a single node. Its a set of all matching nodes at the level of the database where you made the query. The fact that there is only one item in this case because of your equalTo doesn't really matter. (e.g. imagine how this code works without your equalTo call in the query -- it can't, since it doesn't know which item in the snapshot to get the userEtudList child from!). This is why when you try to get the userEtudList child the snapshot is empty. Doc Reference

Calling getValue() on a snapshot returns the Java object representation of the data. If no data exists at the location, calling getValue() returns null.

To fix this, you should probably assert that dataSnapshot.getChildrenCount() == 1 and then:

Iterable<DataSnapshot> stagedata = dataSnapshot.getChildren()
    .iterator().next()
    .child("userEtudList").getChildren();

If you were able to somehow rely on the key name always being equal to uniqueId (its not clear to me why you are storing uniqueId as something other than the key anyway // possibly, this is just an artifact of using a screenshot of the database) you could avoid this by:

Iterable<DataSnapshot> stagedata = dataSnapshot.child(stageid)
    .child("userEtudList").getChildren();

Of course, in this case, the entire query can be removed and you can just retrieve this node directly in the first place.

Upvotes: 1

Omar Skouri
Omar Skouri

Reputation: 33

So i figured it out this is what worked for me

DatabaseStage = FirebaseDatabase.getInstance().getReference("Stages").child(stageid).child("userEtudList");
DatabaseStage.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            mData.clear();
            for(DataSnapshot data:dataSnapshot.getChildren()){
                mData.add(data);

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599946

Firebase Database queries can have at most one unknown key. So unless you know either the 2fded...fd47 key of the 0 key, there's no way to look up the specific node you've marked in red.

Upvotes: 0

Related Questions