NotifyDataSet Changed Android - Recycler View Adapter - android

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.

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 .

add this function to your adapter instead of calling notify adapter change from activity
fun updateAdapter(users: ArrayList<User>) {
this.users= users
notifyDataSetChanged()
}
and call this function
adapter.updateAdapter(users)

Related

DiffUtil.Callback() not working as the same as notifyDataSetChanged()

I have a message list that contains messages. And I used a regular RecyclerView.Adapter instead of ListAdapter.
ChatFragment
chatViewModel.msgListDisplayLiveData.observe(viewLifecycleOwner, {
chatAdapter.setChat(it)
})
Here is the function to update messageList in adapter.
ChatAdapter
fun setChat(newMessages: List<Message>) {
val diffUtil = ChatDiffUtil(messageList, newMessages)
val diffResults = DiffUtil.calculateDiff(diffUtil)
messageList = newMessages
diffResults.dispatchUpdatesTo(this)
--> Method of using notifyDataSetChanged()
// messageList = newMessages
// notifyDataSetChanged()
}
Previously I am using notifyDataSetChanged() and the list item can be updated instantly, but without any animation.
Thus, I decided to use DiffUtils() and I got the nice animation when my list's item got append/remove.
However, when I want to update one of the item, DiffUtils seems do not have any visible effect.
Using notifyDataSetChanged() - can instantly update the row.
Using DiffUtils() - cannot update instantly.
What had I done wrongly? Please advise me.

KOTLIN I can't delete data from sqlite database kotlin RecyclerView

I've a recyclerview and I want to delete a recyclerview item.
fun deleteItem(position: Int){
ModelList.removeAt(position)
notifyItemRemoved(position)
notifyDataSetChanged()
}
override fun onBindViewHolder(holder: myRecyclerViewAdapter.ViewHolder, position: Int) {
holder.bind(ModelList[position])
holder.itemView.listRowDelete.setOnClickListener {
deleteItem(position)
}
I deleted items with this method but because of these items are in sqlite database they all come back when I restard the app. I can't reach positions from my mainactivity and I cant reach database inside of adapter. How can I delete specific item from sqlite database which I want to. Please help thank you
For onClickListener , Don't rely on position from the onBindViewHolder
Instead get the position from holder like this
holder.itemView.listRowDelete.setOnClickListener {
deleteItem(holder.adapterPosition)
}
InClickListeners and things that happen afterwards , get the accurate position with adapterPosition , hover over the adapter position to see the quick documentation !
If you want to call database method , I would suggest you to not call it in Adapter & rather have methods to set custom listeners to adapters which can be called with the victim (item supposed to be deleted)
have a function like this in your adapter
Define the listener property like this
private val myListener:MyCustomListenerInterface = MyCustomListenerInterface{}
Have a setter function to set listener
fun setListener(l:MyCustomListenerInterface){
myListener = l
}
interface MyCustomListenerInterface{
fun onDelete(model)
}
Then in your onClickListener call the onDelete of the custom listener interface like this
holder.itemView.listRowDelete.setOnClickListener {
myListener.onDelete(modelItem)
}
Now When Initializing the adapter for recycler view in fragment/activity , set the listener !!

How to update RecyclerView using Kotlin and Room?

I am trying to display data from my Room database on RecyclerView, it works fine except when I fill in new data and try to update my RecyclerView it doesn't update. Here's part of the code that I tried using:
MainActivity.kt
val adapter = MainAdapter(myData)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = adapter
MainAdapter.kt
fun refresh(data: List<NewList>) {
val currentList: MutableList<MyList> = mutableListOf()
currentList.clear()
currentList.addAll(data)
notifyDataSetChanged()
}
I am trying to call this function from Fragment file so it can pass updated list from Room Database (It updates list once someone opens that fragment)
Fragment.kt
var myData = App.instance.db.getAllDao().getAll()
val adapter = MainAdapter(myData)
adapter.refresh(myData)
I printed updated list in the updateData() function and it does show updated list, as well as tried printing ItemCount and it also shows updated ItemCount, but does not update actual display with new data.
The adapter attached to the RecyclerView in MainActivity and in fragment are not same.
just delete adapter creation in Fragment and pass RecyclerView instance to Fragment and then set layout manager and adapter there instead of activity. lastly call updateData method in order to be able to populate adapter.

Last days I was trying to add api pagination for my recycler view. But I faced a problem such

override fun setAdapter() {
adapter = WaybillAdapter(items, activity!!, this, recyclerView)
recyclerView.adapter = adapter
}
I am updating my adapter when I should add items for my recycler like
override fun addWaybills(list: ArrayList<Data>) {
items.addAll(list)
adapter.setLoaded(true)
}
Add my position of an recycler is jumping to start.
My question is how should update adapter by not changing current position?
In your activitie's onCreate or fragment's onCreateView create initialize your adapter with empty ArrayList and later when you fetch your data add items to your arraylist and call adapter.notifyDataSetChanged()

Should notify data-set-change out side of adapter?

I'm doing notifying data-set-change inside of Adapter.
like,
fun setItems(items: MutableList<IMyModel>) {
list = items
notifyDataSetChanged()
}
fun updateItems(pos: Int: item: IMyModel) {
list[pos] = item
notifyItemChanged(pos)
}
But in many tutorials, I can see they do outside of the Adapter.
adapter.setItems(items)
adapter.notifyDataSetChanged()
So I just wondered if there is any reason I should notify that outside of Adapter? like a bad practice?
Usually, you should call notify directly after changing the dataset, no matter in which class that change occurs.

Categories

Resources