2

I am trying to update ArrayList which is inside in MutableLiveData. whenever I change the content in ArrayList, ListAdapter is not updating.

ViewModel

private var _tileNumberList:MutableLiveData<ArrayList<TileNumber>> = MutableLiveData()

var tempTileNumberList: ArrayList<TileNumber> = ArrayList()

val tileNumberList:LiveData<ArrayList<TileNumber>>
get() = _tileNumberList

init {
    for (i in 1..9){
        for (j in 1..9){
            //tileNumberList.add(TileNumber(0,i,j))
            tempTileNumberList.add(TileNumber(0,i,j))
        }
    }

 fun onStart() {

        tempTileNumberList[count].number = 8
        _tileNumberList.value = tempTileNumberList
        count++

    }

whenever I call onStart method list values changes but ListAdapter not updating with latest values

MainActivity

backTrackingViewModel.tileNumberList.observe(this, Observer {
               it?.let {
                   //adapter.submitList(null)
                   adapter.submitList( it)
               }
            Toast.makeText(
                applicationContext,
                "Hey",
                Toast.LENGTH_SHORT
            ).show()
        })

when i set adapter.submitList(null) then my ListAdapter is updating

TileNumber

data class TileNumber(var number:Int,val row:Int,val column:Int) {
}

I tried this link references but didn't work

ListAdapter not updating item in RecyclerView

https://stackoverflow.com/a/50062174/6925888 (this is not a good solution)

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

iamkdblue
  • 3,448
  • 2
  • 25
  • 43

4 Answers4

0

I think you should initialize tileNumberList list like this

var tileNumberList: LiveData<ArrayList<TileNumber>> =  _tileNumberList

And then observe it in MainActivity just like you're doing it

qki
  • 1,769
  • 11
  • 25
0

I have two possible options why this code doesn't work:

  1. Items are changed in place - i.e. you change the items inside the list. Since the list is passed to adapter, the adapter's list is changed as well. Solution:

    _tileNumberList.value = tempTileNumberList.toList()

  2. You haven't overridden equals() and hashCode() for TileNumber class.

Mike
  • 2,547
  • 3
  • 16
  • 30
  • Thanks, Mike, let the correct item position is not going to change but only content going to change. (on the same position of list), I tried this but no luck. – iamkdblue Feb 10 '20 at 10:55
  • You should also try setting item directly: tempTileNumberList[count] = TileNumber(number = 8) – Mike Feb 10 '20 at 11:15
0

According to ListAdapter.SubmitList() documentation, until you pass the list with the same reference you will never update your list. To make it work you can simply invoke:

adapter.submitList(it.toList())
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
random.123
  • 65
  • 8
0

Replace this:

 fun onStart() {

        tempTileNumberList[count].number = 8
        _tileNumberList.value = tempTileNumberList
        count++

    }

with this:

 fun onStart() {

        val tileNumber = tempTileNumberList[count].copy()  
        tileNumber.number = 8 
        tempTileNumberList.set(count, tileNumber)  
        _tileNumberList.value = tempTileNumberList
        count++

    }

Here I am using Kotlin data class copy() method.

Erselan Khan
  • 692
  • 6
  • 13