https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
s

Schadenfreude

01/22/2019, 5:59 PM
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.
Copy code
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)
                }
            }
        }
    }
a

arekolek

01/23/2019, 9:34 AM
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
6 Views