4

How can I add new Items to a MutableLiveData List? I want to build an infinite scrolling Recyclerview. So I have a List with 10 items and after clicking a "load more button" I want to add ten new Items.

This are my lists of products:

private companion object {
        private var _products = MutableLiveData<MutableList<Product>>()
    }

    val products: LiveData<MutableList<Product>> = _products

Ant this is my loading function:

fun loadProducts(category: Category) { // category: String
        _isViewLoading.postValue(true)


        AppService.fetchProducts(category, neuheitenCounter) { success, response ->
            _isViewLoading.postValue(false)
            if(success) {
                when(category) {
                    Category.NEWS -> {
                        if(_products.value == null) {
                            _products.value = response?.products //as List<Product>
                        } else {
                            response?.let {
                                _products.value?.addAll(response?.products)
                            }
                        }

                        neuheitenCounter++
                    }
                }
            }
        }
    }

If I call _products.value = response?.products it fires the observe method in my Activity. But if I call addAll or add, the observer method is not called.

frankenstein
  • 1,057
  • 6
  • 21
Torben G
  • 641
  • 7
  • 21
  • Does this answer your question? [Notify Observer when item is added to List of LiveData](https://stackoverflow.com/questions/47941537/notify-observer-when-item-is-added-to-list-of-livedata) – Valeriy Katkov Jan 24 '20 at 11:34
  • I think it is not correct. They said addAll and set the list to the new one. I just want to add new items without adding the complete list / override the existing one because I make changes via Adapter and it will be removed if I add a complete new list. – Torben G Jan 24 '20 at 11:43
  • It's usual way of updating the RecycleView. I've added an answer with more details, I hope it will help you. – Valeriy Katkov Jan 24 '20 at 12:27

3 Answers3

2

Actually, it's how you should update the adapter

  1. Add new items to the existing list
  2. Notify LiveData by assigning the same list to its value. Here is the same question with the solution.
  3. When your Activity is notified that the list is changed, it should notify the adapter about the changes by calling its notifyDataSetChanged() method.

When you call notifyDataSetChanged() method of the adapter, it compares previous items with the new ones, figures out which of them are changed and updates the RecyclerView. You can help the adapter by calling notifyItemRangeInserted() instead of notifyDataSetChanged(). notifyItemRangeInserted() accepts a range on inserted items, in your case you can calculate it but taking the difference between adapter.itemCount and the productList.size.

Valeriy Katkov
  • 11,518
  • 2
  • 37
  • 69
  • Ah yes, you are right. Sorry. Didnt know that the adapter is so smart that it figures out which of them are changed. Thank you very much. – Torben G Jan 24 '20 at 12:31
0

I had that same issue .I copied data of LiveData in new List then i added new data to new List.And assigned new List to LiveData.I know this not exact SOLUTION but this solved problem.

Try this.

    var tempList= _products.value
    tempList.addAll(response?.products)
    _products.value = tempList

I hope this will help you.

frankenstein
  • 1,057
  • 6
  • 21
0

MutableLiveData.postValue(T value)