androidandroid-adapternotifydatasetchanged

NotifyDataSet Changed Android - Recycler View Adapter


    val recyclerView = findViewById<RecyclerView>(R.id.rv)
    val adapter = UserDetailsAdapter(users)
    recyclerView.adapter =adapter
    recyclerView.layoutManager= LinearLayoutManager(this)
    Thread{
        userDao.insertAll(user,user2,user3)
        users= userDao.getAll() as ArrayList
        Log.d("Checking Service",users.size.toString())
        recyclerView.post {
            Log.d("Checking Service","Data set Changed")
            adapter.notifyDataSetChanged()
        }
    }.start()

Here recycler view in the UI does not updating the items . runOnUIThread also tried but not working.

kindly explain why notify data set changed not working. and tell me how to update recycler view adapter from another thread.


Solution

  • You need to update the users data inside the adapter before actually calling notifyDataSetChanged(). In your case you need to update the data source which you initialised in adapter constructor .