How to add new child in Firebase database Android

后端 未结 6 1412
说谎
说谎 2020-12-10 05:22

I\'m trying to add a new child using the DatabaseReference in my Firebase Android app. I\'m doing:

DatabaseReference mDatabase = F         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 05:49

    You'll need to call push() to generate a unique key for the new data:

    DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference() 
    mDatabase.push().setValue(1);
    

    This will append the new value at the end of the list.

    By combining the push() and child() methods, you can create multiple lists of children in the database:

    mDatabase.child("numbers").push().setValue(1);
    mDatabase.child("numbers").push().setValue(53);
    mDatabase.child("numbers").push().setValue(42);
    
    mDatabase.child("letters").push().setValue("a");
    mDatabase.child("letters").push().setValue("z");
    mDatabase.child("letters").push().setValue("c");
    

    See the section append to a list of data in the Firebase documentation.

提交回复
热议问题