Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 items stays the same.

this is my updateMessages method in my adapter:

fun updateMessages(messages: List<MessageReceivedResponseModel>?){
    messages?.let {
       this.messages.clear()
       this.messages.addAll(messages)
    }
    notifyDataSetChanged()
}

also here is complete adapter class, I don't understand what's the problem

class MessagesRecyclerAdapter(private val itemActionListener: IOnMessageItemActionListener) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private val messages = ArrayList<MessageReceivedResponseModel>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.awesome_chat_item, parent, false)
    return MyHolder(view)
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    (holder as MyHolder).updateUI(messages[position])
}

override fun getItemCount(): Int {
    return messages.size
}

fun updateMessages(messages: List<MessageReceivedResponseModel>?){
    messages?.let {
        this.messages.clear()
        this.messages.addAll(messages)
    }
    notifyDataSetChanged()
}

private inner class MyHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
    private val messageTextView: TextView = itemView.findViewById(R.id.chat_from_text_id) as TextView
    private val msgQuantityTextView: TextView = itemView.findViewById(R.id.msg_quantity_txt_id) as TextView

    internal fun updateUI(msg: MessageReceivedResponseModel) {
        messageTextView.text = msg.from
        msgQuantityTextView.text = msg.quantity.toString()
    }
}

}

this is where my adapter and recycler initialization goes

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater?.inflate(R.layout.activity_awesome_chat, container, false)
    recycler = view?.findViewById(R.id.awesome_chat_list_view_id) as RecyclerView
    val layoutManager = LinearLayoutManager(activity)
    recycler?.layoutManager = layoutManager
.....
}

override fun onMessageReceived(messages: List<MessageReceivedResponseModel>){
        adapter?.updateMessages(messages)
        Log.e(TAG, messages.size.toString())
  }
// called in oncreateView of fragment
private fun initRecycler(items: List<MessageReceivedResponseModel>?) {
    adapter = MessagesRecyclerAdapter(this)
    adapter?.updateMessages(items)
    recycler?.adapter = adapter
    Log.e(TAG, items?.size.toString())
}
like image 765
jemo mgebrishvili Avatar asked Nov 08 '22 00:11

jemo mgebrishvili


1 Answers

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

fun startMessageRetrieveRequest(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

like image 64
jemo mgebrishvili Avatar answered Nov 15 '22 06:11

jemo mgebrishvili