Where to call notifyOnDataSetChanged - android

In my code, I have two activities.
One is a list view activity, the other activity is being used to modify the data.
After the data is modified the user is returned to the list view activity.
My question is, where should I call notifyOnDataSetChanged?
Should I call it in the 'resume' method of the list view activity, so that the list is refreshed whenever it is displayed?
Or should I call it from the activity that modifies the data?
I would not know how to do the latter, as the adapter belongs to the list view activity, not to the other activity.
Thanks.

You should call it in the ListView Activity, the method basically queues the list to refresh so it should happen when you get back to the list. Also, as it is an adapter method, you can better guarantee that an instance of the list affected by the adapter is available.

Depending on how you handle your Activities, proabably call it in onResume() or some other method of your ListViewActivity so you will have access to your adapter.
However, another way would be to use startActivityForResult() in your ListViewAcitvity then call notifyOnDataSetChanged() in onActivityResult(), if that is an option you have

Related

how to make the Fragment Refresh itself, from another activity

Every time I want to make my fragment call it's onStart() or onResume(), I do the the next :
getFragmentManager().beginTransaction().replace(R.id.containero1, new Massenger_frag(conversationID)).addToBackStack(null).commit();
is this good for the performance, is there another way to tell the fragment the new information arrived, and it must refresh its UI.
You can keep a reference to a "Massenger_frag" object that was used when doing the fragment replacement. You can also define a method "refresh" in your "Massenger_frag" class and call it from that instance variable. onStart()/onResume() from your fragment would also call the same refresh method to avoid code duplication.
What you are currently doing is not fast and adds a significant overhead as the new fragment is constructed every time and the old one gets deallocated.
the solution is simple: I just use BroadCast sender and receiver, and send the information throw Activity A to Activity B, the Activity B in its onReceive() call a function that refresh its content!
Use interface and create custom EventListeners. Check an example here

how to refresh the elements in a adapter without starting a new activity?

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.

Completing fragment transaction when a task is done

It might be an easy question but I have been working on it for hours and couldn't solve it. Sorry if it was asked before.
In my activity I have 2 Fragments. In one fragment I am getting JSON from server and putting in ArrayList. After putting in ArrayList I have to add another fragment passing first value in ArrayList.
Here is the problem, I want to add Fragment when my ArrayList is completed, after it got all values from server. I am making service call in my onActivityCreated() method, server call is happening in another class and I am getting Bundle in a method called onRequestFinished() and I am putting JSON to ArrayList in this Overridden method.
P.S. I tried to put AsyncTask but I couldn't make it since I have to wait the response from onRequestFinished() method.
How can I handle it and complete Fragment Transaction after filling my ArrayList?
Thanks
I suggest using startActivityForResult in the first fragment. When the parent activity receives the onActivityResult it can then start the second fragment. There are plenty of examples of how to do this on SO and elsewhere.
Even if you do not want to start an activity, you can use the same mechanism to communicate back to the activity when your first fragment has completed e.g.:
Is there a method that works like start fragment for result?
Alternatively you could of course just implement a callback mechanism from your first fragment - but personally, I like the onActivityResult approach.

Android: Refresh an activity

I've just a little question (I hope it's just a little question ;-)).
My current app-project has, of course, a bunch of activities. In one of these activities (I call it DataActivity) I can enter some informationen (stored on a webserver). Another activity (called ListViewActivity) should show all available information stored onto the webserver.
The problem is: My first step is to open the ListViewActivity with a few entries (it works proper at first time). Then I go to the DataActivity to input a new listitem. When I now go back to my ListViewActivity the new entry isn't listed. I think I have to refresh the ListViewActivity ... but I don't exactly know how. ;-)
What can I do to refresh this ListView after adding the new row/listitem, so that all available information will be displayed?
I assume you are calling the init of the list in onCreate. Just implement onResume (or onStart should work as well) and add a refresh() method that recalls the data from your server for your list.
You need to call notifyDataSetChanged() or notifyDataSetInvalidated() of your list adapter when you are back to your ListViewActivity.

How to manage activities in Android?

I identified a problem in changing one activity using tab. In one tab activity I'm adding data to my SQLite database, and in the other tab activity I am displaying them using listview(array adapter). But when I come back to add data after adding new items to SQLite, the newly added records are not updated in my listview.
How do I fix this?
You seem to be pulling the list data from a DB. Is there a reason why you are using an ArrayAdapter instead of a CursorAdapter?
Anyway, you should call notifyDataSetChanged() on your list adapter when the data has changed so it can refresh the view.
you can add code to update your listview (via notifyDataSetChanged or some such) by overriding the onResume() method in your activity which is called whenever the activity is brought back to the foreground.
See http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Categories

Resources