gpt4 book ai didi

firebase - Kotlin Firebase:无法将java.lang.String类型的对象转换为com.example.Model.perizinan类型

转载 作者:行者123 更新时间:2023-12-02 13:36:47 24 4
gpt4 key购买 nike

我正在尝试将数据从Firebase加载到RecyclerView,我需要将数据从Firebase加载到ArrayList,但出现以下错误:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.Model.perizinan

这是我的 :
class perizinan(val imageUrl:String, val namakegiatan:String, val keterangan:String, val waktuKegiatan:String, val presensiIzin:String, val tanggalkegiatan:String)
class izin(val perizinan:List<perizinan>)

这是我的 代码:
fun readFirebaseDatabase(){

val ref= FirebaseDatabase.getInstance().getReference("Admin")
ref.addValueEventListener(object: ValueEventListener {

override fun onCancelled(p0: DatabaseError) {
Log.e("AdminActivity","cancelled")
}

override fun onDataChange(p0: DataSnapshot) {
val uid = FirebaseAuth.getInstance().uid

p0.child(uid!!).children.forEach{

it.children.forEach{

val posts = ArrayList<perizinan>()

for (snapshot in it.children) {
val post = snapshot.getValue(perizinan::class.java)
posts.add(post!!)
}

RecyclerViewAdmin.layoutManager = LinearLayoutManager(this@AdminActivity)
RecyclerViewAdmin.adapter = adminAdapter(izin(posts), this@AdminActivity)

}
}


}
})

}

下面是 我的数据库结构:

enter image description here

最佳答案

之所以出现该错误,是因为尝试从数据库中获取perizinan对象时使用了错误的引用。要解决此问题,请更改以下代码行:

val ref= FirebaseDatabase.getInstance().getReference("Admin")


val ref= FirebaseDatabase.getInstance().getReference()
.child("Admin")
.child(FirebaseAuth.getInstance().uid)
.child(senderId)


p0.child(uid!!).children.forEach{ /* ... */ }


p0.children.forEach{ /* ... */ }

关于firebase - Kotlin Firebase:无法将java.lang.String类型的对象转换为com.example.Model.perizinan类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54457110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com