Notifydatasetchanged Not Working on Recyclerview

notifyDataSetChanged not working on RecyclerView

In your parseResponse() you are creating a new instance of the BusinessAdapter class, but you aren't actually using it anywhere, so your RecyclerView doesn't know the new instance exists.

You either need to:

  • Call recyclerView.setAdapter(mBusinessAdapter) again to update the RecyclerView's adapter reference to point to your new one
  • Or just remove mBusinessAdapter = new BusinessAdapter(mBusinesses); to continue using the existing adapter. Since you haven't changed the mBusinesses reference, the adapter will still use that array list and should update correctly when you call notifyDataSetChanged().

recyclerview notifyDataSetChanged() is not working

Finally its working by calling this method from fragment class once getting API response from the volley library.

I have written following method in ViewModel class.

public void updateList(ArrayList<Class> list){
if(adapter != null) {
adapter.updateList(list);
}
}

And calling this method from fragment class like following.

ArrayList<StatisticsModel> list = (ArrayList) data;
viewModel.updateList(list);

So in the adapter class update method is following.

public void updateList(ArrayList<StatisticsModel> itemList){
list.clear();
// this.list = itemList;
// list.addAll(itemList);
// notifyItemInserted(list.size());
// notifyDataSetChanged();
this.list.addAll(itemList);
// notifyItemInserted(list.size());
notifyDataSetChanged();
}

notifyDataSetChanged() not working on custom RecyclerView adapter

Do not reset the reference to the data object. Instead try something like this:

MainActivity.data.clear();
MainActivity.data.addAll(dbh.ReadNotilist(this));
MainActivity.adapter.notifyDataSetChanged();

notifyDataSetChanged() not updating RecyclerView Adapter

    private fun addListItem() {
val input = EditText(activity)
input.setHint("Enter the name of your new list item")
input.inputType = InputType.TYPE_CLASS_TEXT
activity?.let {
val builder = AlertDialog.Builder(activity)
builder.apply {
setTitle("Add List Item")
setView(input)
setPositiveButton(
"Add"
) { dialog, id ->
val newItem = input.text.toString()
viewModel.addMainListItem(newItem)
adapter.addItem(newItem)
}
setNegativeButton("Cancel"
) { dialog, id ->
dialog.cancel()
}
}
builder.create()
builder.show()
}
}

add Method in the adapter also make list to ArrayList and public visibility

fun addItem(item: String){
if(!list.contains(item)){
list.add(item)
}
}

Or if you want to observe changes from ViewModel then add the observing event in your fragment. ViewModel list observing events is best practice in your case.

notifyDataSetChanged does not update my recycler view

you don't need to set the adapter again to update elements.

Instead as you were trying to do, you should call notifyDataSetChanged() but to do that you have to update the adpater's data first, here is a good way to do that:

1-

add this method to your adapter

public void updateData(final ArrayList<ArrayList<StationArrivalPOJO>> stationArrivalPOJO ) {
mStationPojoList= new ArrayList<>();
mStationPojoList.addAll(stationArrivalPOJO);
}

2-

in your Nearby_ViewPager_Stations inside addAdapter, do the following:

  stationArrivalPOJO.addAll(resultData);
mAdapter.updateData(stationArrivalPOJO); // updating adapter's data
mAdapter.notifyDataSetChanged(); //notifying the adapter

Recyclerview notifyDataSetChanged is not working?

This is the problematic code:

binding?.detailsRecyclerview?.apply {
layoutManager = LinearLayoutManager(context)
adapter = DetailRecyclerAdapter()
}

You are effectively creating a 2nd instance of the DetailRecyclerAdapter, i.e. DetailRecyclerAdapter() instantiated twice.

The code should read:

val detailRecyclerAdapter = DetailRecyclerAdapter()

binding?.checkBox?.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
detailRecyclerAdapter.selectAll()
} else {
detailRecyclerAdapter.unSelectAll()
}
}

binding?.detailsRecyclerview?.apply {
layoutManager = LinearLayoutManager(context)
adapter = detailRecyclerAdapter
}

You want to set your detailsRecyclerView.adapter to the instance you created earlier, not a new instance. I renamed the var from adapter to detailRecyclerAdapter to make this clearer.

Recyclerview notifyDataSetChanged(); not working

So you need to update your code. Based on your deleteCode method your mList is not getting modified on delete. notifyDataSetChanged only works if the underlying list that populates the adapter changes.

So you need to update your mList before calling notifyDataSetChanged

So modify your code as -

holder.binding.adapterDelete.setOnClickListener(view ->
{
//Remove from list
mList.remove(position);
notifyDataSetChanged();
//...delete from db ...
SQLiteDataBaseHandler handler = new SQLiteDataBaseHandler(mContext);
CodeItem codeItem = handler.getCodeItem(code);
if (item != null) {
handler.deleteCode(codeItem);

});

Recyclerview Adapter's notifyDataSetChanged not updating RecyclerView

I solved this issue by digging bit into android lifecycle. As i mentioned above i am facing this issue only when i enter ChatFragment from notification. I was only activity state issue. Code was working. What i was doing i always recreate activity on notification tap ( In Firebase Code ).

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

After digging lots of code and changing multiple flags in Intent. Only android:launchMode="singleTask" solves my issue. Which only create activity if it's not exist. Now its not calling activity onDestroy method when tapping on notification. It's start activity from onStart which avoid activity to recreate.

I add this flag in AndroidManifest's NavigationDrawer activity tag.

 <activity
android:name=".Navigation_Drawer"
android:launchMode="singleTask"
android:theme="@style/AppTheme"/>

These kind of issue is really headache for developers but i don't know that with this single line of code i can solve this issue. Thanks everyone for help.



Related Topics



Leave a reply



Submit