Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Firebase数据库引用在插入新数据后不返回更新的dataSnapshot_Android_Firebase_Firebase Realtime Database_Kotlin - Fatal编程技术网

Android Firebase数据库引用在插入新数据后不返回更新的dataSnapshot

Android Firebase数据库引用在插入新数据后不返回更新的dataSnapshot,android,firebase,firebase-realtime-database,kotlin,Android,Firebase,Firebase Realtime Database,Kotlin,我在我的项目中集成了Firebase实时数据库。当活动打开时,我正在检查用户是否已在数据库中进行了检查,并为此从onCreate()调用checkIsAlreadyCheckIn(empDataRef)方法。检查下面的代码 class EmpProfileActivity : BaseActivity<EmpProfileActivityBinding, EmpViewModel>() { private val timeSdf = SimpleDateFormat("hh

我在我的项目中集成了Firebase实时数据库。当活动打开时,我正在检查用户是否已在数据库中进行了检查,并为此从
onCreate()
调用
checkIsAlreadyCheckIn(empDataRef)
方法。检查下面的代码

class EmpProfileActivity : BaseActivity<EmpProfileActivityBinding, EmpViewModel>() {

    private val timeSdf = SimpleDateFormat("hh:mm a", Locale.getDefault())
    private val dateSdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())

    private lateinit var isCheckIn: String
    private lateinit var checkInTime: String
    private lateinit var checkOutTime: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewModel = EmpViewModel()
        mBinding.empProfileViewModel = mViewModel

        val empDataRef: DatabaseReference =
            mDatabase!!.reference.child(FirebaseKey.NodeEmpCheckInOut)
                .child(dateSdf.format(Date())).child(userSession!!.getUserId())

        checkIsAlreadyCheckIn(empDataRef)

        mBinding.txtCheckIn.setOnClickListener {
            when (isCheckIn) {
                "0" -> {
                    empDataRef.child(FirebaseKey.checkIn).setValue(timeSdf.format(Date()))
                    empDataRef.child(FirebaseKey.checkOut).setValue("")
                    empDataRef.child(FirebaseKey.todayDate).setValue(dateSdf.format(Date()))
                    empDataRef.child(FirebaseKey.isCheckIn).setValue("1")
                }
                else -> showAlert("Something went wrong, Please contact your admin")
            }
        }
    }


    private fun checkIsAlreadyCheckIn(empDataRef: DatabaseReference) {

        empDataRef.addValueEventListener(object : ValueEventListener {
            override fun onCancelled(databaseError: DatabaseError) {
                Log.e("Error:", databaseError.message)
            }

            override fun onDataChange(dataSnapshot: DataSnapshot) {
                for(data:DataSnapshot in dataSnapshot.children){
                    //------------>aftet click that view error will come here 
                    isCheckIn = dataSnapshot.child(FirebaseKey.isCheckIn).value as String 
                }
            }
        })
    }
}
我已经在kotlin文档中检查过了,我认为这意味着dataSnapshot值为null,我们不能转换为字符串

问题:将数据插入firebase并在
addEventValueListener
中回调/触发后,如何更新/接收数据?

toString()
使用非常灵活--请尝试以下操作:-

isCheckIn = dataSnapshot.child(FirebaseKey.isCheckIn).value.toString()
注意:-即使它的
null
--它也会导致--
“null”
(作为字符串)。然后可以相应地添加检查。

toString()
使用非常灵活--请尝试以下操作:-

isCheckIn = dataSnapshot.child(FirebaseKey.isCheckIn).value.toString()
注意:-即使它的
null
--它也会导致--
“null”
(作为字符串)。然后您可以相应地添加支票