Thread
#android-architecture
    s

    Schadenfreude

    3 years ago
    Hey guys, I’m using MVVM in a project (for learning purposes mainly) and I’ve implemented a few Fragments with a ViewModel and an Adapter in Java, and haven’t had problems, but this is the first one I’ve written in Kotlin, and for some reason when I delete an item from the cart, I don’t get a notification in the ViewModel Observer and hence I don’t clear the adapter items. Any ideas why this might be happening?
    Never mind, found out what the problem is. You need to use
    postValue()
    after you make a change.
    private fun getSwipeHandler(context: Context): SwipeToDeleteCallback {
            return object : SwipeToDeleteCallback(context) {
                override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                    val position = viewHolder.adapterPosition
    
                    ecartViewModel.eCart.value?.let { cart ->
                        cart.removeAt(position)
                        ecartViewModel.eCart.postValue(cart)
                    }
                }
            }
        }
    arekolek

    arekolek

    3 years ago
    btw when you call
    adapter.setItems(it)
    I’d expect some issues if you use anything other than
    notifyDataSetChanged
    inside that method (for example
    DiffUtil
    ), because it looks like the list in the adapter is already mutated even before you call
    setItems