Android: Notifydatasetchanged(); Not Working

notifyDataSetChange not working from custom adapter

Change your method from

public void updateReceiptsList(List<Receipt> newlist) {
receiptlist = newlist;
this.notifyDataSetChanged();
}

To

public void updateReceiptsList(List<Receipt> newlist) {
receiptlist.clear();
receiptlist.addAll(newlist);
this.notifyDataSetChanged();
}

So you keep the same object as your DataSet in your Adapter.

Custom adapter notifyDataSetChanged not working

Final code: It works fine. thanks to @Justin Powell

public static void change(ArrayList<HashMap<String, String>> resultback){

ArrayList<carArrayItem> CarPriceItemnew = new ArrayList<carArrayItem>();

for (int i = 0; i < (resultback.size()); i+=6) {

CarPriceItemnew.add(new carArrayItem((resultback.get(i).get("table").toString()),
(resultback.get(i+1).get("table").toString()),
(resultback.get(i+2).get("table").toString()),
(resultback.get(i+3).get("table").toString()),
(resultback.get(i+5).get("table").toString())));

}

CarPriceItem.clear();
CarPriceItem.addAll(CarPriceItemnew);
adapter.notifyDataSetChanged();

}

Android: notifyDataSetChanged(); not working

One of the main reasons notifyDataSetChanged() won't work for you - is,

Your adapter loses reference to your list.

When you first initialize the Adapter it takes a reference of your arrayList and passes it to its superclass. But if you reinitialize your existing arrayList it loses the reference, and hence, the communication channel with Adapter.

When creating and adding a new list to the Adapter. Always follow these guidelines:

  1. Initialise the arrayList while declaring it globally.
  2. Add the List to the adapter directly without checking for null and empty values. Set the adapter to the list directly (don't check for any condition). Adapter guarantees you that wherever you make changes to the data of the arrayList it will take care of it, but never
    lose the reference.
  3. Always modify the data in the arrayList itself (if your data is completely new then you can call adapter.clear() and arrayList.clear() before actually adding data to the list) but don't set the adapter i.e If the new data is populated in the arrayList than just adapter.notifyDataSetChanged()

Stay true to the Documentation.

notifyDataSetChanged not working

just change your myList and call adapter.notifyDataSetChanged() don't set a new adapter each time.

In the constructor of your custom adapter do call super that takes ArrayList as the argument.

call this:

public MiAdaptadorListaComercios(Context c,List<Comercio> myList){
super(c,0,myList);
this.contexto = c;
this.myList = myList;
}

You can keep the adapter as it is the problem is in this line:

myList = filteredList;

instead of changing the reference you should change the list itself.

myList.clear();
myList.addAll(filteredList);

By doing this you will loose your original list to I would suggest keeping another list call ed originalList which will have the complete list and initialize myList in onCreate by:

myList=new ArrayList(originalList);

so every time you want to re-set just call:

myList.clear();
myList.addAll(originalList);

and in

    for(Comercio co : originalList)
{
if(co.getName().contains(filer))
{
filteredList.add(co);
}
}

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 working properly on fragment

Use the below given method inside your NotificationsAdapter.class and then call this method instead of calling notifyDataSetChanged() directly in your Fragment. Actually you are not passing data to the Adapter that was the issue.

public void updateAdapter(ArrayList<Notifications> mDataList) {
this.mList = mDataList;
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();
}


Related Topics



Leave a reply



Submit