android 我的应用程序在尝试创建旁路列表时崩溃

nwsw7zdq  于 2023-06-20  发布在  Android
关注(0)|答案(1)|浏览(89)

我需要创建旁路列表(在cardviw中)这是我如何尝试添加旁路到byPassesList:

val bypassListener = object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        val bypassList: MutableList<Bypass> = mutableListOf()
        for (employeeSnapshot in dataSnapshot.children) {
            val employeeEmail = employeeSnapshot.key.toString()
            for (streetSnapshot in employeeSnapshot.children) {
                val streetName = streetSnapshot.key.toString()
                for (bypassSnapshot in streetSnapshot.children) {
                    val houseNumber = bypassSnapshot.key.toString()
                    val bypass = bypassSnapshot.getValue(Bypass::class.java)
                    bypass?.let {
                        it.userEmail = employeeEmail ?: ""
                        it.street = streetName ?: ""
                        it.houseNumber = houseNumber ?: ""
                        bypassList.add(it)
                    }
                }
            }
        }
        val recyclerView = binding.rvBypasses
        val layoutManager = LinearLayoutManager(this@BypassesList)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView.layoutManager = layoutManager
        val adapter = BypassAdapter(bypassList)
        recyclerView.adapter = adapter
    }

    override fun onCancelled(databaseError: DatabaseError) {
        Toast.makeText(applicationContext, "Ошибка бд", Toast.LENGTH_SHORT).show()
    }
}
val bypassesRef = FirebaseDatabase.getInstance().getReference("Bypasses")
bypassesRef.addValueEventListener(bypassListener)

如果我尝试用Bypasses列表开始我的活动,我有这个(Logcat的日志):
致命异常:主要
进程:com.example.sellerhelper,PID:4295
com.google.firebase.database.DatabaseException:无法将类型为java.util.ArrayList的对象转换为类型为com.example.sellerhelper.Bypass
Bypasses的类看起来像这样:

data class Bypass(
    var userEmail: String = "",
    var street: String = "",
    var houseNumber: String = ""
)

我该怎么弥补呢?也许我需要重写我的旁路类以不同的方式?我尝试将字段类型userEmail,street,houseNumber更改为ArrayList,但不起作用。Sry for dumb question:)

nbnkbykc

nbnkbykc1#

所以,我修复了这个问题,一个问题是在val bypass = bypassSnapshot.getValue(Bypass::class.java)中,我将bypassSnapshot更改为employeeSnapshot(周期中的第一个快照)。
我认为问题在于上一个快照中没有足够的数据,因此出现了类型冲突

相关问题