1

I don't quite understand the behavior of MutableLiveData or I can't find the proper documentation.

But I need help.

  1. I need to add an element to the first position of the array and tried with "plus" but it doesn't work properly. Or is there a way to add multiple elements of an array in the first position?

_series.value = _series.value?.plus(it) // ??? 11111111

  1. I can't figure out how to delete all the elements of the array, I did it with

_series.value = null // ??? 2222222

_series.value = ArrayList() /// ??? 2222222

but I don't think it is the best way to do it.

class SerieViewModel(val database: SerieDatabaseDao) : ViewModel() {
    private val _series = MutableLiveData<List<Serie>?>()
    val series: LiveData<List<Serie>?>
        get() = _series

    fun fetchSeries(code: String) { 
        viewModelScope.launch {
            try {
                val response = CodesApi.retrofitService.getSeries(code)
                val series = response.body()?.data          
                series?.forEach {
                  _series.value = _series.value?.plus(it) // ??? 11111111
                  }
                }

            } catch (e: Exception) {
                Log.d(TAG, "fetchSeries: ${e.message}")
            }    
        }
    }

    fun clearDb() = viewModelScope.launch {
        database.clear() 
        _series.value = null  // ??? 2222222
        _series.value = ArrayList<Serie>()  /// ??? 2222222
    }

thanks

kekaben971
  • 13
  • 3

1 Answers1

1
  1. You are storing a List<Serie?> which is fine thats what you should do, but you can't add elements to List, you need to convert List to ArrayList and add elements to the ArrayList than update the MutableLiveData, follow the code below.

  2. to clear all the elements from the List you can set the value to emptyList(), follow the code below.

Note: Take a look about the difference between List, ArrayList, Array in kotlin they are not the same

class SerieViewModel(val database: SerieDatabaseDao) : ViewModel() {
    private val _series = MutableLiveData<List<Serie>?>()
    val series: LiveData<List<Serie>?>
        get() = _series

    fun fetchSeries(code: String) {
        viewModelScope.launch {
            try {
                val response = CodesApi.retrofitService.getSeries(code)
                val series = response.body()?.data
                val liveDataSeries =
                    if (_series.value == null) emptyList<Serie>() // set the array list to empty if _series.value is null
                    else ArrayList(_series.value) // else: add _series.value elements to the array list
                series?.forEach {
                    liveDataSeries.add(it) // add each serie to the array list
                }
                _series.value = liveDataSeries // update _series.value
            } catch (e: Exception) {
                Log.d(TAG, "fetchSeries: ${e.message}")
            }
        }
    }

    fun clearDb() = viewModelScope.launch {
        database.clear()
        _series.value = emptyList()
    }

}
Mohamed Rejeb
  • 2,281
  • 1
  • 7
  • 16
  • Thanks for the answer, it was very helpful, I don't know why I'm confused about arrays in kotlin. If you look at the code I have a foreach loop, how can I enter one by one in fisrt prosition(prepend) ? because I need to make a check if the SERIES does not exist in the local database. If not exist add to local database and update de recyclerview. Or merge the "liveDataSeries" arraylist that you created with _series.value – kekaben971 Jun 11 '22 at 18:45
  • because _series.value = liveDataSeries overwrites the previus _series.value values – kekaben971 Jun 11 '22 at 18:51
  • @kekaben971 I edited the code, now the elements are aded to the array list with the forEach and also the initial elements of _series.value are added to the array list – Mohamed Rejeb Jun 11 '22 at 19:02
  • thanks again. this line "else ArrayList(_series.value) " Type mismatch . Required: (MutableCollection..Collection) Found: List?. – kekaben971 Jun 11 '22 at 20:46
  • ok, its working like this. But ithink that is not the best solution. val tempArray = ArrayList() ... _series.value?.let { tempArray.addAll(it.toMutableList()) } .... foreach{ tempArray.add(0, it)} .... _series.value = tempArray – kekaben971 Jun 11 '22 at 21:01
  • Yup it's not the best solution but it's a good one you can keep it like that – Mohamed Rejeb Jun 11 '22 at 21:26