android - 响应 onChildAdded、Firebase 的名称值是多少?

标签 android firebase firebase-realtime-database

我在我的项目中使用 Database Firebase。我对 onChildAdded 非常困惑。我有这样的代码:

mEventListener = new ChildEventListener() {
              @Override public void onChildAdded(@NonNull DataSnapshot dataSnapshot,
                @Nullable String stringS) {
   if (stringS != null
       && dataSnapshot.getKey()
      != null && !dataSnapshot.getKey().contains(stringS)) 
    {
     Log.e("=====", "onChildAdded: " + stringS +
           " getKey: " + dataSnapshot.getKey());
    }....
  • 我在这里得到了日志:

=====: onChildAdded: -LMsqXJTn5DoMZTELzuz getKey: -LMsqXJVyXWZKqDizrCt

  • 那么这个“dataSnapshot”的关键是什么???

My firebase

  • 在其他情况下我也得到了一些相反的结果,所以这让我很困惑。

添加:

  • 一些其他的DataSnapshot,它们都是一样的。

最佳答案

onChildAdded 有两个参数:

  • 刚刚添加的DataSnapshot
  • previous child 的键,之后添加了这个新的数据快照

因此在您的示例中,新快照具有 key -LMsqXJVyXWZKqDizrCt 并且它添加在 -LMsqXJTn5DoMZTELzuz 之后。

另见 reference documentation for onChildAdded :

public abstract void onChildAdded (DataSnapshot snapshot, String previousChildName)

This method is triggered when a new child is added to the location to which this listener was added.

Parameters

snapshot - An immutable snapshot of the data at the new child location

previousChildName - The key name of sibling location ordered before the new child. This will be null for the first child node of a location.

关于android - 响应 onChildAdded、Firebase 的名称值是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52434189/

相关文章:

android - android gradle plugin 3.0.0-alpha1版本太旧

javascript - 如何处理 catch 函数?

ios - Firebase iOS/Swift 和深层链接

firebase - _firebase2.default.database.ref 不是函数

java - 尝试从 Firebase 检索数据并收到此错误 : SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

android 是否可以写保护我使用 StorageAccessFramework 创建的纯文本文件?

java - 微调器不显示所选项目

java - jsonbin.io 获取 Json 数据滞后问题

android - Firebase 电话身份验证不适用于自己的设备

ios - 在两个类之间使用 NSDictionary 值