【问题标题】:How to reference firebase database in to another activity (used for updating db records)? [duplicate]如何将firebase数据库引用到另一个活动(用于更新数据库记录)? [复制]
【发布时间】:2018-05-14 09:57:43
【问题描述】:

我有一个活动 A,其中创建了允许添加数据的 firebase db。

现在,我想在 android studio 中创建另一个 Activity B,它允许将记录更新到在 Activity A 中创建的 firebase db。

我该怎么办?

【问题讨论】:

  • 您必须将 DB 对象的引用传递给 Activity B。或者,您可以在 Activity A 中创建公共方法并从 Activity B 调用它们。
  • 你能给我举个例子,说明如何在Activity B中传递DB对象的引用吗?

标签: android database firebase-realtime-database


【解决方案1】:

在 Activity B 中添加对位置的引用:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("location_where_you_saved");

Map<String, Object> updates = new HashMap<>();
updates.put("name", newName);
ref.updateChildren(updates);

如果在活动 A 中,您创建了一个名为 Books 的节点并添加了 namedate 等属性,然后在活动 B 中添加对子 Books 的引用,并使用新数据更新记录如上所述。

https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2020-07-02
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多