Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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
Kotlin Android RecyclerView NotifyDataSetChanged with LiveData_Kotlin_Android Recyclerview_Android Livedata - Fatal编程技术网

Kotlin Android RecyclerView NotifyDataSetChanged with LiveData

Kotlin Android RecyclerView NotifyDataSetChanged with LiveData,kotlin,android-recyclerview,android-livedata,Kotlin,Android Recyclerview,Android Livedata,为我的RecyclerView实例化ListAdapter时,我调用: viewModel.currentList.observe(viewLifecycleOwner){ adapter.submitList(it) } 这就是我在Android Sunflower项目中看到的将LiveData提交给RecyclerView的正确方式。这很好,当我向数据库添加项目时,它们会在RecyclerView中更新。然而,如果我写 viewModel.curre

为我的
RecyclerView
实例化
ListAdapter
时,我调用:

    viewModel.currentList.observe(viewLifecycleOwner){
        adapter.submitList(it)
    }
这就是我在Android Sunflower项目中看到的将
LiveData
提交给
RecyclerView
的正确方式。这很好,当我向数据库添加项目时,它们会在
RecyclerView
中更新。然而,如果我写

    viewModel.currentList.observe(viewLifecycleOwner){
        adapter.submitList(it)
        adapter.notifyDataSetChanged()
    }
然后我的适配器数据观察器总是落后。我打电话

adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver(){
    override fun onChanged() {
        super.onChanged()
            if (adapter.currentList.isEmpty()) {
                empty_dataset_text_view.visibility = View.VISIBLE
            } else {
                empty_dataset_text_view.visibility = View.GONE
            }
        }
})
empty\u dataset\u text\u view
在列表大小为零后会出现一个更改,同样,在列表大小为非零后会消失一个更改

显然,观察者在提交
it
之前正在运行代码,在本例中,它是从
Room
查询的
LiveData
。我只想知道解决方法是什么:有没有办法“等待”LiveData的返回