Reputation: 57
I want to read the values in location and store it in an array. But when I run the code I get empty values in the arrays. Here I am trying to read the values in the -MgvP468FQ-jclZWHC5S
node. How to read the values and store it in the array. I am not getting any values in 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) {
}
})
Upvotes: 0
Views: 184
Reputation: 599196
As far as I can see you have a few mistakes in the handling of the data.
This should be closer:
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
...
}
}
}
Upvotes: 1