简体   繁体   中英

How to get a child's data when it has been pushed?

I am using the "push()" function to store objects into my firebase database and since it stores data with unique keys I can't change the stored objects values using what's mentionned in the documentation:

myRef.child("child name").setValue(model);

because I simply don't know the key. I wan wondering is there an other way to modify the data of firebase database.

I want to add a new child called "conversations" to the users and programmatically add childs to him .

and this is the databse structure:

在此处输入图片说明

If you have the following database:

 Users
    pushId
        name : john
        age  : 100

In case you dont know the pushId , you can do the following:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()){
         String key = ds.getKey();
         String name = ds.child("name").getValue().toString();
     }
}

 @Override
public void onCancelled(DatabaseError databaseError) {
 }
});

The above will get you the name and the key using getKey()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM