Ankesh Kushwah
Ankesh Kushwah

Reputation: 161

Update List View immediately


I have a custom list view. I want to update this list view when user enters new data (I used a dialog to insert data). I successfully did it by calling

    adapter.notifyDataSetChanged();

Every thing is working fine. The only problem is that i want my data (in list view ) updated immediately as the dialog dismisses

{Listview(activity) and Dialog(simple class extending DialogFragment) are in two seperate class files} .

Currently the data updates only when user interacts with listview activity after entering the data. Is there any way to call onResume of ListView activity (I put update function in onResume) immediately as the dialog dismisses.

Upvotes: 0

Views: 219

Answers (2)

Hardik4560
Hardik4560

Reputation: 3220

What i deduced from the above description is, You have activity A as the listActivity and activity B as the dialog activity. In activity A there is some button which triggers activity B and a dialog is shown. Dismissing the dialog you want to notify activity A. If my assumption is right I have a solution for your problem.

Try any of the following:

  1. In the onWindowFocusedChanged() of activity A, notifyDataSetChanged() if the hasFocus value is true;

  2. Activity A should subscribe for activity B, when inside activity B dialog is dismissed call the observer method of activity A.

  3. or You can ask activity A to launch activity B for some result by calling startActivityForResult passing in the request code. When activity B finishes it will send a call back to onActivityResult method.

Upvotes: 0

gian1200
gian1200

Reputation: 3854

If I understand correctly, you have an activity A with a ListView. Activity A opens activity B. Activity B has a dialog. You want to update the ListView after the user do some interaction with the dialog, right?

One approach is to call adapter.notifyDataSetChanged() (the one you are using) inside activity A's onResume(). This happens every time the onResume method is called, even if there is nothing to update.

Another approach will be to make Activity B send a message to activity A to let him know that the ListView need to be updated. you can do this by using startActivityForResult and onActivityResult methods. More info here and here.

Upvotes: 1

Related Questions