DiffUtil ItemCallback areContentsTheSame() always returns true after updating an item on the ListAdapter

前端 未结 4 619
灰色年华
灰色年华 2020-12-18 04:13

While using a ListAdapter I noticed that after updating an item the DiffUtil.ItemCallback areContentsTheSame() method was always returning true. De

4条回答
  •  盖世英雄少女心
    2020-12-18 04:48

    I have 0 reputation so I can't comment, but @Shadow's answer worked for me.

    I am also working with Room + LiveData + ViewModel + DiffUtil and editing the contents of a list item with a dialog. Although java is pass by value, when we pass a value of an object were actually passing the reference to it. So when you edit the contents of the same object in say a dialog you're actually changing the same reference of the list item, hence why DiffUtil cannot calculate difference when LiveData does an onChange.

    This explains java pass by value/reference better: Is Java "pass-by-reference" or "pass-by-value"?

    What I did:

    Item newItem = new ItemBuilder().setId(oldItem.getId()).......

    Then make changes to newItem, then update the DB from viewModel

提交回复
热议问题