我在我的项目中使用 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”的关键是什么???
- 在其他情况下我也得到了一些相反的结果,所以这让我很困惑。
添加:
- 一些其他的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/