无需执行代码即可进行控制

时间:2019-08-31 13:47:35

标签: android firebase firebase-realtime-database kotlin

我正在从getRideBookedDetails()调用函数onCreate()来从Firebase Realtime Database获取详细信息。我还能够从Firebase获取数据并将其打印在logcat上。但是控制流不是从上到下,而是在没有执行几行代码的情况下结束。

请在函数中查看,直到此代码可以正常执行,但在此之后,如果执行了块,则按代码弹出Toast:

                                println(toAddressFB)
                                println(toAddressFB)
                                println(date)
                                println(time)
                                println(noOfSeats)
                                println(noOfSeatsBooked)

功能:

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

0 个答案:

没有答案