【问题标题】:How to add new data to Firebase database in android?如何在 android 中向 Firebase 数据库添加新数据?
【发布时间】:2018-11-04 13:22:11
【问题描述】:

问题本身比问题看起来更复杂,至少在我看来。

我的数据库如下所示:

问题是我对将 Firebase 用于我的 android 应用程序相当陌生,我不知道如何在此处添加全新的用户配置文件。例如,如果有人要注册,然后它会在“2”处添加一个新的“记录”,依此类推。你怎么做到这一点?

【问题讨论】:

  • 您也可以签出this

标签: java android firebase firebase-realtime-database


【解决方案1】:

这取决于您存储数据的方式。如果你有一个类,它的对象中包含所有值,那么你可以使用这样的代码来做到这一点:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("userTable");
// where User is your class and user is it's object with the values you need to update
ref.child(user.getNumber()).setValue(user);

如果您没有这样的类并且需要设置这些值,那么您必须对数据库的每个节点单独执行此操作,代码如下:

DatabaseReference refNew = FirebaseDatabase.getInstance().getReference().child("userTable").child(i);

// where i is the variable which you have to keep count of your users

ref.child("Age").setValue(age);
ref.child("Company").setValue(company); // and so on

// age, company are variable with values you want to store

【讨论】:

    猜你喜欢
    • 2016-09-30
    • 2018-07-05
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    相关资源
    最近更新 更多