ElvisBoss
ElvisBoss

Reputation: 199

Doing a Query in Android for Firebase inside push keys

i have a FirebaseRecyclerAdapter that was working before i structured my firebase, now i am not sure how to get the same info with the newly structure json.

before i have this:

DatabaseReference userReference = mRootRef.child("Users");
query = userReference.orderByChild("name");

which worked with the following structure

"Users" : {
"dq92R8ftK2haPHJSA9undgqpmxh1" : {
  "name" : "Elvis De Abreu",
  "username" : "elvis0288"
}

now i added a new node called Info like this:

"Users" : {
"Ox0s6GhGhDcZ1vKkteeZfIyNYXI2" : {
  "Info" : {
    "email" : "[email protected]",
    "name" : "Elvis De Abreu",
    "username" : "111"
  }
}

i have tried to put .child("Info") and other related alternatives but that is not working. should i removed the Info and leave just the first structure or there is a way to do this? the thing is that after Info i will have another node for Games so this is why i want to have it separated

i would really appreciate some help

Thanks!

Upvotes: 1

Views: 205

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138814

You should not remove the Info, you need to use it in a correct way. You need to change this child:

.child(Info)

with

.child("Info")

See the double quote within parenthesis.

Please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String email = ds.child("Info").child("email").getValue(String.class);
            String name = ds.child("Info").child("name").getValue(String.class);
            String username = ds.child("Info").child("username").getValue(String.class);
            Log.d("TAG", email + " / " + name + " / " + username);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);

Your output will be:

[email protected] / Elvis De Abreu / 111

Upvotes: 1

Related Questions