gpt4 book ai didi

java - FirebaseDatabase onChildChanged 返回多个快照

转载 作者:行者123 更新时间:2023-12-02 09:16:36 26 4
gpt4 key购买 nike

我有一个问题,我不能只得到最近添加的 child 。

这是我的数据库:

这是我的测试代码:

val db = FirebaseDatabase.getInstance().reference
val newref = db.child("/Orders/ODbPagPuDFgGhj0l73lynD4Wdkr2/")


newref.addChildEventListener(object: ChildEventListener {
override fun onChildChanged(snap: DataSnapshot, p1: String?) {
Log.d("ac1234","${snap}")
}


override fun onCancelled(p0: DatabaseError) { }

override fun onChildMoved(p0: DataSnapshot, p1: String?) { }


override fun onChildAdded(p0: DataSnapshot, p1: String?) { }

override fun onChildRemoved(p0: DataSnapshot) { }

})

这是 logcat:

2019-11-19 06:02:24.596 14991-14991/com.example.fru_vegetablesadmin D/ac1234: DataSnapshot { key = -Lu2y9uay-7G3gtFI4g3, value = {locationLat=33.95212290629996, locationLong=35.62042236328125, phoneNumber=+19823737, totalPrice=8,197 L.L., optionalAddress=, optionalNote=, time=19/10/2019 16:2 , order={Fresh Cucumber={totalQuantity=3 Kg, totalPrice=7497}}} }
2019-11-19 06:02:24.603 14991-14991/com.example.fru_vegetablesadmin D/ac1234: DataSnapshot { key = -Lu2y9uay-7G3gtFI4g3, value = {locationLat=33.95212290629996, locationLong=35.62042236328125, phoneNumber=+19823737, totalPrice=8,197 L.L., optionalAddress=, optionalNote=, time=19/10/2019 16:2 , order={Fresh Cucumber={totalQuantity=3 Kg, totalPrice=7497}}} }
2019-11-19 06:02:24.645 14991-14991/com.example.fru_vegetablesadmin D/ac1234: DataSnapshot { key = -Lu2y9uay-7G3gtFI4g3, value = {locationLat=33.95212290629996, locationLong=35.62042236328125, phoneNumber=+19823737, totalPrice=8,197 L.L., optionalAddress=, optionalNote=, time=19/10/2019 16:2 , order={Fresh Cucumber={totalQuantity=3 Kg, totalPrice=7497}, Fresh Eggplant={totalQuantity=200 G, totalPrice=700}}} }

我不知道为什么它返回3行数据,我只想听是否在uid下添加了一个 child (“ODbPagPuD...”)

提前致谢

最佳答案

更改此:

        newref.addChildEventListener(object: ChildEventListener {
override fun onChildChanged(snap: DataSnapshot, p1: String?) {
Log.d("ac1234","${snap}")
}


override fun onCancelled(p0: DatabaseError) { }

override fun onChildMoved(p0: DataSnapshot, p1: String?) { }


override fun onChildAdded(p0: DataSnapshot, p1: String?) { }

override fun onChildRemoved(p0: DataSnapshot) { }

})

进入此:

newref.addValueEventListener(object: ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
Log.d("ac1234","${dataSnapshot}")

}

override fun onCancelled(databaseError: DatabaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException())

}
}

关于java - FirebaseDatabase onChildChanged 返回多个快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58936112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com