简体   繁体   English

孩子的数据被推送后如何获取?

[英]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:我正在使用“push()”函数将对象存储到我的 firebase 数据库中,并且由于它使用唯一键存储数据,因此我无法使用文档中提到的内容更改存储的对象值:

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.我想知道还有其他方法可以修改 firebase 数据库的数据。

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:如果您不知道pushId ,您可以执行以下操作:

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()以上将使用getKey()您提供name和密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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