Shahzad Imam
Shahzad Imam

Reputation: 2018

Refreshing Activity When Dialog Dismisses

Actually in my list activity,when user taps on any item ,a dialog box is opened with two options of delete and cancel.When user select delete button the dialog dismisses with deleting value in database.And the old list activity comes in foreground with old values.Though it is deleted it is showing in listview.Is there a way to show the refreshed list.If yes,then How?

Upvotes: 2

Views: 11815

Answers (7)

Revertron
Revertron

Reputation: 1362

You can override activity method onWindowFocusChanged(boolean hasFocus) and track the state of your activity.

Normally, if some alert dialog is shown above your activity, the activity does not get onPause() and onResume() events. But it loses focus on alert dialog shown and gains it when it dismisses.

In your case you can refresh your data in onWindowFocusChanged() if hasFocus is true.

Upvotes: 0

Akram
Akram

Reputation: 7527

If you are using array list then you must definitely know the item position and using that position you can remove item from your list .Then call the

     yourlist.remove(position);
     adapter.notifyDataSetChanged();

run your program to watch the magic.

Upvotes: 1

King RV
King RV

Reputation: 3770

Try this, implement the dialog dismiss listener and check if the user has clicked on your "delete" button, if yes then perform your refresh

        dialog.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {

            }
        })

Refresh the listview by setting the apdater again....

Upvotes: 1

waqaslam
waqaslam

Reputation: 68187

you need to remove the deleted item from adapter too before calling adapter's notifyDataSetChanged() in order to refresh your list

Upvotes: 2

user1203673
user1203673

Reputation: 1015

clear the list using list.clear(); and set the adapter like this adapter = new Adapter(this); enter code herelistView.setAdapter(adapter);

Upvotes: 0

barry
barry

Reputation: 4147

When closing the Dialog call

yourAdapter.notifyDataSetChanged()

on your list adapter.

Upvotes: 1

Altaaf
Altaaf

Reputation: 527

Please try following code,

private final Handler handler = new Handler();

make a function called: doTheAutoRefresh() that does:

private void doTheAutoRefresh() {
    handler.postDelayed(new Runnable() {
             doRefreshingStuff(); // this is where you put your refresh code
             doTheAutoRefresh();
         }, 1000);
}

Call this function in your onCreate.

NOTE: this is the basic approach. consider stopping this after onPause has been called and to resume it after onResume. Look at the handler class to see how to remove.

Upvotes: 1

Related Questions