What is the difference? The android documentation doesn't have a description for notifyDataSetInvalidated(). I was thinking maybe you call that function to notify all registered listeners, but use notifyDataSetChanged() to not notify them?
Changed means the data set changed. Individual items updated, or items were added or removed. Invalidated means the data source is no longer available.
Related
I am using the PagedList library in my app.
It all works as expected, using the PagedListAdapter. However, I am not able to find how I can get a callback and be notified that the PagedList has been updated.
At list's ItemKeyedDataSource is used to fetch the list's data from the network. At that point, the PagedListAdapter's submitList is called, providing a PagedList of length 0. When the DataSource has fetched the data from the network, its callback.onResult() is executed and the list's UI is updated showing the fetched items. However, this does not call the submitList method, and I have not found a way to be notified in the adapter of this update, as the onCurrentListChanged is neither called. How can the Adapter be notified of such changes?
Thank you :)
You can use PagedList.addWeakCallback. You can pass a PagedList.Callback to it and listen to events such as onInserted, onChanged or onRemoved. More detail can be found in this answer
There is no mention of why. I still have to code all the view holder stuff, so what is the benefit of using it instead of RecyclerView.Adapter?
Just curious.
It automatically adds a RealmChangeListener to your RealmResults, thus automatically calling adapter.notifyDataSetChanged() when the underlying elements are written to. Therefore you don't need to write the code to keep your adapter in sync with the results.
I'm moving my application from sqlite to Firebase. Previously I would read N items from the DB to an arraylist and call notifyItemRangeInserted. Now the most convenient way to get data from Firebase delivers objects one by one. I was wondering if anyone benched the cost of calling notifyItemInserted for each list item. Is that fine or should I batch my loads? I'm displaying everything in a RecyclerView.
Based on the documentation for RecyclerView:
There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred. Structural changes are when items are inserted, removed or moved within the data set.
notifyItemInserted : Notify any registered observers that the item reflected at position has been newly inserted
notifyItemRangeInserted : Notify any registered observers that the currently reflected itemCount items starting at positionStart have been newly inserted
Its a bit tricky to quantify and compare these two.
Based on the functionality: Both of them seems to perform same operation. They don't alter the existing items binding but rather alter existing items position.
Based on impact after notify observer: Calling notifyItemInserted in short interval would trigger the registered observers frequently. If those observers are doing some heavy computation then notifyItemInserted for each item would be expensive.
However, if you do have list that is not sequential in a range (e.g. remove item 1, 2 and 4), or if you want to perform insertion and removal simultaneously, and have it animated accordingly or you frequently modifying the adapter's dataset, efficient way would be to implement DiffUtil which is available in support library.
Most of the time our list changes completely and we set new list to
RecyclerView Adapter. And we call notifyDataSetChanged to update
adapter. NotifyDataSetChanged is costly. DiffUtil class solves that
problem now. It does its job perfectly
You can find more information here.
I have a ListFragment whose data is populated by a custom adapter ( a SimpleAdapter in my case). I was experiencing issues with using notifyDataSetChanged() from within my class that extended ListFragment. After a lot of looking around and several (useful) Stack Overflow posts later:
listview not updating with notifydatasetchanged() call
Android ListView not refreshing after notifyDataSetChanged
adapters notifyDataSetChanged does not work
notifyDataSetChanged not working
ListView does not update when calling notifyDataSetChanged from a BaseAdapter
I understand that a loose (and highly un-recommended) workaround would be to re-set your adapter using setListAdapter(). However I am now facing issues with this as well.
The documentation, http://developer.android.com/reference/android/app/ListFragment.html#setListAdapter(android.widget.ListAdapter), mentions that setListAdapter()
Provides the cursor for the list view.
But I still have some questions.
Q1. Does initializing an adapter multiple times using setListAdapter() 'point' to the same adapter instance ?
Q2. What actually happens when a call is made to getListAdapter() and then to notifyDataSetChanged() when an adapter has been set multiple times using setListAdapter() ?
Q3. This question is based on an assumption from Q2- when notifyDataSetChanged() is called when an adapter is set multiple times, which of those adapter instances (this part is the assumption), if they exist' is actually being notified for change ?
I am a beginner with Android and I believe there a quite a few nuances I do not understand.I would be extremely grateful if you could clarify these questions. Also thank you very much for your time.
Q1. Does initializing an adapter multiple times using setListAdapter() 'point' to the
same adapter instance ?
Ans: Initializing the adapter will point only to the last instance that you set using setListAdapter.
Q2. What actually happens when a call is made to getListAdapter() and then to
notifyDataSetChanged() when an adapter has been set multiple times using
setListAdapter() ?
Ans: It doesn't matter how many adapters that you have initialized, only the last instance will be retrieved using the getListAdapter().When you use notifyDataSetChanged() only the last instance wich is retrieved using getListAdapter() will be refreshed i.e. ; the last instance will be reloaded(By calling the getView).
Q3. This question is based on an assumption from Q2- when notifyDataSetChanged() is
called when an adapter is set multiple times, which of those adapter instances (this
part is the assumption), if they exist' is actually being notified for change ?
Ans: The above answer contains the explanation for this.
I was looking at the answer for making a Custom CursorLoader, and was wondering that if the data changes would there will be a notification of the data changing so that the list updates?
I'm using the compatibility library loader framework. When content changes I just call this and everything gets updated:
getSupportLoaderManager().getLoader(YOUR_LOADER).onContentChanged();
You could call notifyDataSetChanged on your adapter so that the ListView will display the updated cursor content.
From the documentation:
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.