Notifydatasetchange Not Working from Custom Adapter

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 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();

notifyDataSetChange() Is not working in adapter

you are creating new adapter instance every time instead of updating already created and attached to RecyclerView. move your creation code into if for creating and attaching adapter only once, then in else you can update it (same reference, same object)

if (page == 1) {
adapter = new NotificationAdapter(getActivity(), response.body().getData().getData());
binding.rvNotification.setAdapter(adapter);
} else {
adapter.updateList(response.body().getData().getData());
}

another approach would be to create new adapter every time but then you also must call setAdapter(adapter); (new one). updating already existing one is better approach

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);
}
}

notifyDataSetChange not working from adapter

First Add ArrayList of CommentModel,
Then Add Your Adapter an pass List via constructor of adapter

CommentAdapter adapter;
ArrayList<CommentModel> list;

Inside Parsing

list = new ArrayList<CommentModel>();
list.add(newCommentModel)
adapter = new CommentAdapter(list,this);


Related Topics



Leave a reply



Submit