Skip to content Skip to sidebar Skip to footer

Notifydatasetchanged() Doesn't Call's Onbindviewholder() Method

I have a RecyclerView's Adapter, where I am adding items dynamically, when I am calling the my adapter's updateMessages function old data list were changing correctly but, recycler

Solution 1:

I realized my problem where out of all this classes, The problem was in my interactor class where the messages retrieving requests started

funstartMessageRetrieveRequest(callback: OnRequestFinishedListener<List<MessageReceivedResponseModel>>){
    doAsync {
        Thread.sleep(1000)
        val idx = Random().nextInt(2)
        val its: List<MessageReceivedResponseModel>
        when(idx){
            0 -> its = REs
            1 -> its = RES_1
            else -> its = RES_1
        }
        callback.onResponse(its)
    }
}

I removed doAsync and works correctly, here callback.onResponse() is being called from Non-UI thread and it caused the problem Only the original thread that created a view hierarchy can touch its views., but not always. Also app weren't crashed and I missed the log

Solution 2:

Try using notifyItemRangeChanged(0, messages.size - 1);.

funupdateMessages(messages: List<MessageReceivedResponseModel>?){
    messages?.let {
        this.messages.clear()
        this.messages.addAll(messages)
        this.notifyItemRangeChanged(0, messages.size - 1)
    }
}

I think notifyDataSetChanged() isn't working because your dataset (messages field) is still the same object.

You can try to replace object of messages:

funupdateMessages(messages: List<MessageReceivedResponseModel>?){
    messages?.let {
        this.messages = messages
        notifyDataSetChanged()
    }
}

Solution 3:

You should use notifyDataSetChanged(); on your adapter, like this: adapter.notifyDataSetChanged(); . If you wanna use a class you can, you just need to declare the adapter variable as a member variable of the class and call the method like I said above. Where adapter is the name of your adapter.

Post a Comment for "Notifydatasetchanged() Doesn't Call's Onbindviewholder() Method"