0


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.

Ankesh Kushwah
  • 161
  • 1
  • 3
  • 12
  • When do you call the `adapter.notifyDataSetChanged();`? Try calling it in the `onResume()` of the `ListView` activity.. what is the problem? – Amulya Khare Nov 21 '13 at 04:53
  • 1
    http://developer.android.com/reference/android/content/DialogInterface.OnDismissListener.html . use `OnDismissListener` and then update listview using `adapter.notifyDataSetChanged()`. – Raghunandan Nov 21 '13 at 04:53
  • @AmulyaKhare of course i called it in onResume the problem is onResume must be called immediately after user enters the data and dialog dismisses. – Ankesh Kushwah Nov 21 '13 at 04:56
  • @Raghunandan nice solution but still same problem. I tried but same old, same old. – Ankesh Kushwah Nov 21 '13 at 05:12

2 Answers2

1

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.

Community
  • 1
  • 1
gian1200
  • 3,670
  • 2
  • 30
  • 59
  • Well this is a nice solution but my dialog isn't an activity. I'm sorry that i forget to mention this above. This is simply a class that extends DialogFragment and returns alertDialog.create when i called it from my listview activity by using dialog.show(getSupportFragmentManager(), MY_DIALOG); – Ankesh Kushwah Nov 21 '13 at 05:20
  • That is even easier to do. What you can do is create a method inside the class that extends DialogFragment. This method/setter should pass a ´Runnable´ that will execute when the user enters new data. This will help you trigger almost any code as if you were on Activity A. – gian1200 Nov 21 '13 at 05:31
  • Agreed but i already did it in a separate class file to avoid overheads. And i don't want to change it. And i have a solution now -> onWindowFocusedChanged(); – Ankesh Kushwah Nov 21 '13 at 05:48
  • Glad you fix it. Just a reminder, `onWindowFocusedChanged` will execute more times than needed - when the window gains and loses focus - rather than just the required times. If you find some performance issues in the future, this may be the cause. – gian1200 Nov 21 '13 at 06:02
  • I am using a flag to check whether data changed or not. List will update only when flag is set otherwise not. – Ankesh Kushwah Dec 04 '13 at 13:59
0

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.

Hardik4560
  • 3,202
  • 1
  • 20
  • 31