无法在 kotlin 的 firebase 实时数据库中读取嵌套子节点
Failure to read nested child node in firebase realtime database in kotlin
我想读取 location 中的值并将其存储在数组中。但是当我 运行 代码时,我在数组中得到空值。在这里,我试图读取 -MgvP468FQ-jclZWHC5S
节点中的值。如何读取值并将其存储在数组中。我在 lat1
、long1
.
中没有得到任何值
var ref1= FirebaseDatabase.getInstance().getReference("location").child("oBwEgUfhMbQoPPovRXkgWJcHz6B2")
ref1.addValueEventListener(object : ValueEventListener{
override fun onDataChange(p0: DataSnapshot) {
if (p0.exists()){
for (idsnapshot in p0.children){
for (locsnapshot in idsnapshot.child("-MgvP468FQ-jclZWHC5S").children){
val lat1 = locsnapshot.child("latitude").value as Double
val long1 = locsnapshot.child("longitude").value as Double
Log.d("TAG", "$locsnapshot")
Toast.makeText(requireContext() ,"success$locsnapshot", Toast.LENGTH_SHORT).show()
Toast.makeText(requireContext() ,"success$lat1,$long1", Toast.LENGTH_SHORT).show()
lat = lat1.toDouble()
val LatLng = LatLng(lat1, long1)
list.add(LatLng)
}
}
}
}
override fun onCancelled(p0: DatabaseError) {
}
})
据我所知,您在处理数据时存在一些错误。
这应该更接近:
ref1.addValueEventListener(object : ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()){
for (childsnapshot in snapshot.child("-MgvP468FQ-jclZWHC5S").children){ // children location8 and location9
for (locsnapshot in childsnapshot.children){ // children 0..4
val lat1 = locsnapshot.child("latitude").value as Double
val long1 = locsnapshot.child("longitude").value as Double
...
}
}
}
我想读取 location 中的值并将其存储在数组中。但是当我 运行 代码时,我在数组中得到空值。在这里,我试图读取 -MgvP468FQ-jclZWHC5S
节点中的值。如何读取值并将其存储在数组中。我在 lat1
、long1
.
var ref1= FirebaseDatabase.getInstance().getReference("location").child("oBwEgUfhMbQoPPovRXkgWJcHz6B2")
ref1.addValueEventListener(object : ValueEventListener{
override fun onDataChange(p0: DataSnapshot) {
if (p0.exists()){
for (idsnapshot in p0.children){
for (locsnapshot in idsnapshot.child("-MgvP468FQ-jclZWHC5S").children){
val lat1 = locsnapshot.child("latitude").value as Double
val long1 = locsnapshot.child("longitude").value as Double
Log.d("TAG", "$locsnapshot")
Toast.makeText(requireContext() ,"success$locsnapshot", Toast.LENGTH_SHORT).show()
Toast.makeText(requireContext() ,"success$lat1,$long1", Toast.LENGTH_SHORT).show()
lat = lat1.toDouble()
val LatLng = LatLng(lat1, long1)
list.add(LatLng)
}
}
}
}
override fun onCancelled(p0: DatabaseError) {
}
})
据我所知,您在处理数据时存在一些错误。
这应该更接近:
ref1.addValueEventListener(object : ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()){
for (childsnapshot in snapshot.child("-MgvP468FQ-jclZWHC5S").children){ // children location8 and location9
for (locsnapshot in childsnapshot.children){ // children 0..4
val lat1 = locsnapshot.child("latitude").value as Double
val long1 = locsnapshot.child("longitude").value as Double
...
}
}
}