Barath R
Barath R

Reputation: 209

How to iteratively retrieve data from Firebase using Android?

I have 2 questions actually:

  1. How do I iteratively get the names of all user names from first to last user?

  2. How can I retrieve data from all the sibling nodes from one ID. Suppose I enter xyz, the query should return number:99, car1....etc.)


User1{
  name: xyz
  number: 99...
  car1{ regno: 123
        make: ghj   }
  car2{.....}
 }
User2{..... }

Upvotes: 0

Views: 80

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80924

Q1>How do I iteratively get the names of all user names from first to last user?

  DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");

reference.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
    for(DataSnapshot datas: dataSnapshot.getChildren()){
      String names=datas.child("name").getValue().toString();
     }
  }
 @Override
public void onCancelled(DatabaseError databaseError) {
     }
});

Here you have the snapshot at Users then you iterate inside each user and retrieve the names, assuming you have this database:

 Users
   user1
     name:xyz
   user2
     name: xziq

Q2>How can I retrieve data from all the sibling nodes from one ID.(Suppose I enter xyz, the query should return number:99, car1....etc)

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users");
Query queries=ref.orderByChild("name").equalTo("xyz");
queries.addListenerForSingleValueEvent(new ValueEventListener() {...}

To retrieve the sibling nodes, then you need to use a query like orderByChild("name").equalTo("xyz") then attach a listener and retrieve the other nodes.

Upvotes: 1

Related Questions