I am calling an function getRideBookedDetails()
from onCreate()
to get the details from Firebase Realtime Database. I am also able to get the data from firebase and printing it on logcat. But the control flow is not going in from Top to Bottom manner instead it goes at the end without execution of few line of code.
Please see in function, till this code executing fine but after this last if block gets executed and pop up a Toast as per code:
println(toAddressFB)
println(toAddressFB)
println(date)
println(time)
println(noOfSeats)
println(noOfSeatsBooked)
Function:
private fun getRideBookedDetails() {
// boolean variable to show error msg only one time
var msgShowNew = true
val getDataReference = mDatabase.getReference("Find_ride")
getDataReference.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
Log.w("Get Data Error:", "getDataReference:onCancelled", p0.toException())
Toast.makeText(applicationContext, "Action Cancelled", Toast.LENGTH_SHORT).show()
}
override fun onDataChange(p0: DataSnapshot) {
println(p0)
println("children: " + p0.children)
println("key: " + p0.key)
println("value: " + p0.value)
var sno = 0
// creating for loop to get data until ends
for (data in p0.children) {
// creating hashmap to get each value inside children separately using hash key
val hashMap = data.value as HashMap<String, Any>
//println("Ride By UID "+hashMap["ridebookedByUserID"])
if (hashMap.size > 0) {
if (hashMap["ridebookedByUserID"]!!.equals(storedUID)) {
sno++
val rideCreatedKeyRef = hashMap["createRideKeyRef"]
val noOfSeatsBooked = hashMap["numOfSeatRequired"]
//println("Ride created key Ref: " + rideCreatedKeyRef)
//println("Seats: " + noOfSeatsBooked)
// getting ride created by user data using uid
val getUserData = mDatabase.reference.child("Create_ride/$rideCreatedKeyRef")
getUserData.addListenerForSingleValueEvent(object: ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
Log.w("Get Data Error:", "getData:onCancelled", p0.toException())
}
override fun onDataChange(p0: DataSnapshot) {
// getting value using hash map keys, key in ["..."] should be same as created // uncomment above printline to know key or see Firebase
val fromAddressFB = p0.child("fromaddress").value.toString()
val toAddressFB = p0.child("toaddress").value.toString()
val date = p0.child("ridestartdate").value.toString()
val time = p0.child("ridestarttime").value.toString()
val noOfSeats = p0.child("noofseatsmax").value
val noOfFilledSeats = p0.child("nooffilledseats").value
println(fromAddressFB)
println(toAddressFB)
println(date)
println(time)
println(noOfSeats)
println(noOfSeatsBooked)
**Till here code execution perfectly then control is going to if(msgShowNew == true) { ... }**
============================================================================
// binding data to class rideDetails
val ride = rideDetails() // rideDetails is class which is used to transport data/bind data
ride.sno = sno
ride.rideFrom = fromAddressFB
ride.rideTo = toAddressFB
ride.rideDate = date
ride.rideTime = time
ride.numOfRider = noOfSeatsBooked!!
rideDetails.add(ride)
msgShowNew = false
}
})
}
}
}
if(msgShowNew == true) {
Toast.makeText(applicationContext, "You have not created/taken any ride yet.", Toast.LENGTH_LONG).show()
}
// turning off visibility of progress bar
rideHistoryProgressBar!!.visibility = View.GONE
}
})
}
Logcat
08-31 19:07:24.314 28261-28261/com.example.carpooleasy I/System.out: DataSnapshot { key = Find_ride, value = {-LnbnXT1NYY9ghXsMbN6={ridebookedByUserID=8CX0A7Z8FLbPGVQuIrk2ZtLgl4n2, createRideKeyRef=-Lnbn5uj5LUjtY2LafdM, numOfSeatRequired=2}} }
08-31 19:07:24.314 28261-28261/com.example.carpooleasy I/System.out: children: com.google.firebase.database.DataSnapshot$1@ebe9b5
08-31 19:07:24.315 28261-28261/com.example.carpooleasy I/System.out: key: Find_ride
08-31 19:07:24.315 28261-28261/com.example.carpooleasy I/System.out: value: {-LnbnXT1NYY9ghXsMbN6={ridebookedByUserID=8CX0A7Z8FLbPGVQuIrk2ZtLgl4n2, createRideKeyRef=-Lnbn5uj5LUjtY2LafdM, numOfSeatRequired=2}}
08-31 19:07:24.717 28261-28261/com.example.carpooleasy I/System.out: Block E, Shyam Vihar Phase-1, Najafgarh, Delhi, 110043, India
08-31 19:07:24.717 28261-28261/com.example.carpooleasy I/System.out: Pocket 7, Sector 12 Dwarka, Dwarka, New Delhi, Delhi 110075, India
08-31 19:07:24.718 28261-28261/com.example.carpooleasy I/System.out: 31/08/2019
08-31 19:07:24.718 28261-28261/com.example.carpooleasy I/System.out: 21:30
08-31 19:07:24.718 28261-28261/com.example.carpooleasy I/System.out: 4
08-31 19:07:24.718 28261-28261/com.example.carpooleasy I/System.out: 2