【问题标题】:Failure to read nested child node in firebase realtime database in kotlin在 kotlin 的 firebase 实时数据库中读取嵌套子节点失败
【发布时间】:2021-10-16 21:25:34
【问题描述】:

我想读取 location 中的值并将其存储在一个数组中。但是当我运行代码时,我在数组中得到空值。在这里,我试图读取-MgvP468FQ-jclZWHC5Snode 中的值。如何读取值并将其存储在数组中。我在lat1long1 中没有得到任何值。

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) {

    }

})

【问题讨论】:

    标签: arrays kotlin firebase-realtime-database children


    【解决方案1】:

    据我所知,您在处理数据时存在一些错误。

    这应该更接近:

    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
              ...
            }
          }
        }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多