Rehan Sarwar
Rehan Sarwar

Reputation: 1004

How to get nested Child from Firebase Database Using Android?

I want to get list of all Allowed child from this type of JSON Tree:

databaseRef.child('Users').child('Allowded').addValueEventListener(new ValueEventListener() {

@Override
public void onDataChange (DataSnapshot dataSnapshot) {

  //
  }
}

@Override
public void onCancelled (DatabaseError databaseError) {

} };);

enter image description here

Upvotes: 1

Views: 2630

Answers (3)

Gnana Sreekar
Gnana Sreekar

Reputation: 11

databaseRef.child('Users').child('Allowded').addValueEventListener(new ValueEventListener() {

@Override
public void onDataChange (DataSnapshot dataSnapshot) {

   for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
        Log.d(TAG, "onDataChange: 1    " + childDataSnapshot.getKey());
            for (DataSnapshot childDataSnapshot2 : childDataSnapshot.getChildren()){
                Log.d(TAG, "onDataChange: 2    " + childDataSnapshot2.getKey());
            }
   }

  }
}

@Override
public void onCancelled (DatabaseError databaseError) {

} };);

Upvotes: 0

Ahmad
Ahmad

Reputation: 201

Firebase listeners fire for both the initial data and any changes.

If you're looking to synchronize the data in a collection, use ChildEventListener. If you're looking to synchronize a single object, use ValueEventListener. Note that in both cases you're not "getting" the data. You're synchronizing it, which means that the callback may be invoked multiple times: for the initial data and whenever the data gets updated.

FirebaseRef.child("message").addValueEventListener(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot snapshot) {
  System.out.println(snapshot.getValue());  //prints "Do you have data? You'll 
    love Firebase."
 }
@Override
 public void onCancelled(DatabaseError databaseError) {        
}
 });

Upvotes: 1

Vijay Makwana
Vijay Makwana

Reputation: 486

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference userRef = database.getReference("users").child(key).child("Alloweded");
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    User userObj = dataSnapshot.getValue(User.class);

}

@Override
public void onCancelled(DatabaseError databaseError) {
    // Getting Post failed, log a message
    Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
    // ...
}
};userRef.addValueEventListener(postListener);

User is your Model class which have lat, lng, name,no., profileUrl etc

Try this I hope it works fine.

Upvotes: 2

Related Questions