SumOne
SumOne

Reputation: 827

How to query/get data under one node android firebase?

my database structure looks like:

myDatabasename
name1:{
    UserID: { 
       uniqueId: {
         Data1: ""
         Data1: ""
    }

    UserID: { 
       uniqueId: {
         Data1: ""
         Data1: ""
    }
}
name2:{
    UserID: { 
       uniqueId: {
         Data1: ""
         Data1: ""
    }

    UserID: { 
       uniqueId: {
         Data1: ""
         Data1: ""
    }

I would like to retrieve data from just name1 part of the node but I am not sure how I can do this: Previously I did not have name1 and name2, so I was able to retrieve data like this; which worked fine.

 for (DataSnapshot ds1: ds.getChildren()) {
        DATA data= ds1.getValue(DATA.class);
        datas.add(data);
    }

I am new to firebase, so I am not sure how I can do this, I tried playing around, but it underlines ds.getChildren("name1") in red

for (DataSnapshot ds1: ds.getChildren("name1")) {
            DATA data= ds1.getValue(DATA.class);
            datas.add(data);
        }

DATA data = ds1.equals("name1").getValue(DATA.class); // I tried this but it also throws red line under getValue;

==== EDIT

Sorry for the confusion before the structure above, my structure looked like and I was able to read this by using the code above.

UserID: { 
  uniqueId: {
     Data1: ""
     Data1: ""
   }
  uniqueId: {
     Data2: ""
     Data2: ""
   }
} 

Upvotes: 2

Views: 828

Answers (1)

Janice Kartika
Janice Kartika

Reputation: 502

You can iterate through all children of name1 using:

for(DataSnapshot ds1 : dataSnapshot.getChildren()){
    // your codes
}

But you should point the listener to name1 as reference.

databaseReference.child("name1").addListenerForSingleValueEvent(valueEventListener);

Or, you also can do this:

for(DataSnapshot ds1 : dataSnapshot.child("name1").getChildren()){
    // your codes
}

And create a listener:

databaseReference.addListenerForSingleValueEvent(valueEventListener);

You can use other listener methods.

Upvotes: 1

Related Questions