【问题标题】:LiveData is duplicating MutableListLiveData 正在复制 MutableList
【发布时间】:2020-05-29 04:58:10
【问题描述】:

我想在 Firestore 中发生数据更改时更新我的​​ Recyclerview 项目。 所以我的活动有以下代码:

taskViewModel.fetchedTaskLiveData.observe(
   this, Observer {
    if (it != null) {
     todoListAdapter.setListData(it)
     showRecyclerView()
   }

内部适配器setListData方法:

fun setListData(data: MutableList<Todo>) {
        //this.todoList.clear()
        this.todoList.addAll(data)
        notifyDataSetChanged()
    }

在这种情况下,每当获取更改的列表时,Recyclerview 都会复制并添加更改的列表 在旧列表下方列出。

如果“this.todoList.clear()”未注释,则列表将被清除,并且数据更改时不会显示任何数据。

我尝试了所有可能的解决方案,但我认为某处缺少一部分。卡了很久。 非常感谢各种建议。

【问题讨论】:

  • 我推荐this.todoList = ArrayList(data),然后是notifyDataSetChanged()
  • @EpicPandaForce 试过了。但是没用
  • 可能您的 RecyclerView 高度不正确,例如 wrap_contentsetHasFixedSize(true)
  • 是的 @EpicPandaForce 它目前是 height="wrap_content" 和 recyclerview.setHasFixedSize(true)。我需要改变吗?
  • 是的。让它setHasFixedSize(false)

标签: android mvvm android-livedata mutablelivedata


【解决方案1】:

所以我一直坚持这一点,并在 12 小时后发现了一个微小的变化。 我通过将 for 更改为 forEach 来纠正它,这改变了游戏规则。

我也在添加项目之前清除列表项。

todoList.clear()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2021-10-12
    • 2020-11-10
    • 1970-01-01
    • 2017-12-25
    相关资源
    最近更新 更多