0

我正在使用带有实时数据的房间数据库

从数据库中检索信息

private fun getData(query: String) {
    var dataIssueJson: MutableList<DataIssue>
    dataViewModel.searchDatabase(query).observe(this, {
        dataList = it as MutableList<Data>

        data = if (dataList.isNotEmpty())
            dataList[0] else
            Data()
        val gson = Gson()
        val dataIssueString =
            if (dataList.isNotEmpty()) {
                dataList[0].dataIssue
            } else ""

        val type = object : TypeToken<MutableList<DataIssue>>() {}.type

        dataIssueJson = if (dataIssueString.isNotEmpty())
            gson.fromJson(dataIssueString, type)
        else
            mutableListOf()
        viewAdapter.clear()
        initRecyclerView(dataIssueJson.toDataIssueItem())

        val dataStatus = if (dataList.isNotEmpty())
            dataList[0].dataStatus
        else "Status"

        dataViewModel.dataStatus.value = dataStatus
        colorStatus(dataStatus)
    })
}

回收站视图

private fun initRecyclerView(dataItem: List<dataIssueItem>) {
    binding.recyclerViewDataIssues.apply {
        layoutManager = LinearLayoutManager(this@MainActivity)
        adapter = viewAdapter.apply {
            addAll(botItem)
            setOnItemClickListener(onClickItem)
            notifyDataSetChanged()

        }
        scrollToPosition(binding.recyclerViewDataIssues.adapter!!.itemCount - 1)
    }
}

private fun List<DataIssue>.toDataIssueItem(): List<DataIssueItem> {
    return this.map { DataIssueItem(it) }
}

点击

private val onClickItem = OnItemClickListener { item, view ->
    if (item is DatatIssueItem) {
        Log.d(AppConstants.MAIN_ACTIVITY_TAG, "DataIssue:${item.dataIssue.dataIssue}, date&time: ${item.dataIssue.dateAndTimeOfIssue}")

    }
}

Recyclerview 工作正常,但是当我单击 Recyclerview 项目时,它返回一个空项目,我只是不知道为什么

4

1 回答 1

0

是的,我想通了。数据来自 Item 本身,因此请确保从 Item 可以访问数据。

于 2021-08-09T01:46:04.877 回答