In the document for the method notifyDataSetChanged of class BaseAdapter noted that "Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself."
Supposed I changed the 3rd element in my string array (array data for the ListView), what "any View reflecting the data set should refresh itself" means ? Does the 3rd view item in my list view be notified ?
Also, how is notifyDataSetChanged() and getView() concerned ?
Supposed I changed the 3rd element in my string array (array data for
the listview), what "any View reflecting the data set should refresh
itself" means ?
It means that any view which shows/is based on/uses that data(the string array in your case) should be invalidated(remeasured, redrawn) in order to show the user the new set of data.
Does the 3rd view item in my list view be notified?
No, the parent ListView will be notified. When you set the adapter on a ListView, an observer(from the ListView) will be set for that adapter. Calling notifyDataSetChanged on the adapter will announce that observer from the ListView that something has happen to the data. At this moment the ListView will recreate the rows to show the new data.
Also, how is notifyDataSetChanged() and getView() concerned ?
I'm not sure I understand what you ask. The getView method of an adapter is used by the ListView to obtain a new row each time this is required. When you call notifyDataSetChanged on the adapter this will trigger the observer in the ListView. As it's time to recreate the list, the ListView will call the getView method of the adapter to show the necessary number of rows(the ones visible on the screen). So each time you call notifyDataSetChanged the getView method will be called for the visible rows.
Related
I have viewPager() which have 3 pages, it have a recyclerView and each page have three different adapters and i want something like when user clicks on adapter item in first page it adds in a database and second adapter gets its array list from the same database, but it shows in second page's adapter after app restarts, so the question is how to update the second page's adapter without restart.
A regular ListView can use a CursorAdapter which provides some of the functions you need. Alas, there is no existing CursorAdapter class that extends RecyclerView.Adapter.
The quickest solution would be to use RecyclerViewCursorAdapter on GitHub.
Then it would work something like this:
Update your database
Get a reference to your second page fragment. You can store this reference when you create the fragment in getItem(). Note that your fragment could be null depending on what pages the ViewPager has/hasn't created.
Call a method on your fragment that will create a new Cursor to be used by the adapter (with the same query as before). When the adapter receives the new Cursor it will update the RecyclerView.
i used list adapter for setting values to listview.The values are loaded from webservice api.I need how to refresh the customized listview with list adapter in android.
There are two ways to do that:
Create a new adapter and set it to the ListView with setAdapter every time you reload your data.
Reuse the adapter (via setDataor similar methods) and invoking notifyDataSetChanged after that, so the view is refreshed.
You need to use notifyDataSetChanged() from the adapter, in order to do that your adapter needs extend BaseAdapter, so every time you change some data call the ,
instanceOfAdaper.notifyDataSetChanged()
I guess you must be using AsyncTask method to fetch data from WebService,Just override this method,and call notifyDataSetChanged method in onPostExecute like this.
protected void onPostExecute(String result) {
adapter.notifyDataSetChanged();
}
notifyDataSetChanged updates the view/adapter when you change the object.
But how to have the object updated when the view is changed by the user (Checkbox checked)
I am trying to put the Checkbox onclick listener in the newView(...) and my adapter is in separate file.
My worry is that if I update the array object only internally then when the notifidatasetchanged is called the update will be lost. Is it true? Can I update the main object found in the Activity from the newView() found in the adapter in different file?
When the CheckBox is checked/unchecked, the onClick method for that CheckBox will be called. When that happens, you can change the object in the backing data for your adapter depending on whether the CheckBox is checked or not.
Please, go over the official tutorial: http://developer.android.com/guide/topics/ui/controls/checkbox.html
I am trying to display a list fragment in the middle of my activity. The list fragment adapter is a custom adapter (extended from BaseAdapter) with the typical ViewHolder pattern. It is implemented correctly.
I have the adapter set up with greenrobot Eventbus to receive a new List object from an asynctask which does the query in the background (as not to slow down the UI Main Thread).
The problem is the list fragment doesn't have the results of the database query initially so it defaults to empty (and displays the textview in my xml for the main activity which has the id 'empty').
In the end, my adapter, and listviewfragment don't get instaniated at all because it defaults to empty.
Is there a better method to doing this?
How can I get my listview fragment to wait for the data recieved from the asyncTask?
I am going to just do a fragment with a listview in it, instead of a listview fragment, and I'll see if that will help.
As I understood correctly, you did everything okay so far. What you have to do is to add an "update" method to your adapter, and call this whenever your asynctask finished it's job and got the results for you.
This update method should look something like
public void updateAdapterData(List<String> newList) {
mItems.clear(); // say mItems is the global list you have in the adpater
mItems.addAll(newList);
notifyDataSetChanged(); // This will cause the adpater to re-draw it's rows, so now data should be visible in your list
}
And in your fragment you do somehting like:
adpater.updateAdapterData(newList);
Does this help?
I have the following situation:
I have a singleton controller which contains a List<Foo>
when my activity starts I load the items to my BaseExpandableListAdapter
the problem: When I now delete a item form the singleton List it gets net deletet in the view. Only when I start the whole activity again. How to solve this without starting the whole activity again?
public void notifyDataSetChanged ()
Added in API level 1
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
http://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged%28%29
When you change the database that is used by the adapter, you should call notifyDataSetChanged() on the adapter.