使用Kotlin从firebase realtime检索/获取嵌套节点对象

dkqlctbz  于 2023-04-12  发布在  Kotlin
关注(0)|答案(2)|浏览(133)

我想从嵌套节点中的日期获取数据对象(address,check_in,check_out)。
我的数据库是这样的:

---`attendance` :{
   |
   |---danielle:{ <---------- here is username, node of `attendance`
   |   |
   |   |---2023-04-08: <----------- here is the date she checked in and out for work
   |   |   |
   |   |   |---address: "Thomas street"
   |   |   |
   |   |   |---check_in: "09:02 AM"
   |   |   |
   |   |   |---check_out: "06:30 PM"
   |   |
   |   |
   |    ---2023-04-10":
   |       |
   |       |---address: "Maria street"
   |       |
   |       |---check_in: "09:02 AM"
   |       |
   |        ---check_out: "06:30 PM"
   |
    ---mark:{ <---------- here is another username
       |
       |---2023-04-07: <----------- here is the date he checked in and out for work
       |   |
       |   |---address: "Thomas street"
       |   |
       |   |---check_in: "09:02 AM"
       |   |
       |   |---check_out: "06:30 PM"
       |
       |
        ---2023-04-11":
           |
           |---address: "Maria street"
           |
           |---check_in: "10:23 AM"
           |
            ---check_out: "08:30 PM"

我试着用这个编码

val attModel = arrayListOf<AttendanceModel>()

Firebase.database.getReference("Attendance").child("danielle").child("2023-04-08").addValueEventListener(object : ValueEventListener {
                override fun onDataChange(snapshot: DataSnapshot) {
                    for (h in snapshot.children) {
                        val data = h.getValue(AttendanceModel::class.java)

                        attModel.add(data!!)

                    }
                }
            }

但我得到这个错误

Android : com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to AttendanceModel

我知道danielle确实是一个String,但我不明白它怎么会得到一个错误,因为danielle内部有一个对象节点。
我还在这里尝试了这种编码,我尝试为路径2023-04-08嵌套

val todays = arrayListOf<AttendanceModel>()

        Firebase.database.getReference("Attendance").child("danielle")
            .addValueEventListener(object : ValueEventListener {
                override fun onDataChange(snapshot: DataSnapshot) {
                    for (h in snapshot.children) {
                        val date = h.child("2023-04-08")
                        for (i in date.children) {
                            val dataUser = i.getValue(AttendanceModel::class.java)

                            todays.add(dataUser!!)
                        }

在模拟器不返回任何东西,然后我试图记录dataUser,它给我

DataSnapshot { key = danielle, value = null }

这是我的数据类

data class AttendanceModel(
    val loc_name: String? = "",
    val address: String? = "",
    val check_in: String? = "",
    val check_out: String? = ""
)

任何形式的帮助将非常感谢!

b4qexyjb

b4qexyjb1#

不需要迭代快照子对象,因为已经获取了整个对象的日期。如果我们使用迭代,它将返回对象中每个键的值。
替换

for (h in snapshot.children) {
val data = h.getValue(AttendanceModel::class.java)
 attModel.add(data!!)
 }

作者

val data = snapshot.getValue(AttendanceModel::class.java)
            attModel.add(data!!)
wz3gfoph

wz3gfoph2#

当你在2023-04-08上附加一个监听器时,不需要迭代,你可以直接读取子节点的值,或者你可以将节点Map到一个AttendanceModel类型的对象中。在代码中,它将像这样简单:

val db = Firebase.database.reference
val dateRef = db.child("attendance/danielle/2023-04-08")
dateRef.get().addOnCompleteListener {
    if (it.isSuccessful) {
        val snapshot = it.result
        val key = snapshot.key
        Log.d(TAG, "key: $key")
        val data = snapshot.getValue(AttendanceModel::class.java)
        Log.d(TAG, "data: ${data.address}${data.check_in}/${data.check_out}")
    } else {
        Log.d(TAG, "${it.exception?.message}") //Never ignore potential errors!
    }
}

logcat中的结果将是:

key: 2023-04-08
data: Thomas street/09:02 AM/06:30 PM

相关问题