简体   繁体   English

当关闭另一个活动的警报对话框时,更新主活动中的列表视图

[英]Update listview in main activity when alert dialog of another activity gets dismissed

I have a dialog activity which sets up an alert dialog every time the alarm goes off. 我有一个对话活动,每次警报响起时都会设置一个警报对话框。 Now this alert dialog can be infront of any activity. 现在,此警报对话框可以位于任何活动的前面。 As it gets called by the alarm manager when the alarm is supposed to go off. 当警报应该响起时,由警报管理器调用。 The problem is that if this alert dialog comes infront of the main activity which holds the list of upcoming alarms then when I "dismiss" the alarm, that alarm should get removed from the list( which does get deleted) but after the alert dialog disappears the main activity doesn't get updated unless I resume the activity or go to next activity and then back to main activity. 问题是,如果此警报对话框位于包含即将发生的警报列表的主要活动的前面,那么当我“关闭”警报时,该警报应从列表中删除(确实会删除),但是在警报对话框消失之后除非我继续该活动或转到下一个活动然后返回到主要活动,否则主要活动不会更新。

I tried calling the main activity inside the onDismiss method of the dialog like this: 我试图像这样在对话框的onDismiss方法内调用main活动:

 alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {

                ((MainActivity) context).customAdapter.notifyDataSetChanged();
        }
    });

However I get an exception that my DialogActivity's context can't get converted into main activity's context. 但是我得到一个例外,即我的DialogActivity的上下文无法转换为主要活动的上下文。 How can I solve this? 我该如何解决?

Here's my Dialog Activity class 这是我的Dialog Activity类

public class DialogActivity extends AppCompatActivity {

AlarmManager alarmManager;
PendingIntent pendingIntent;
Intent startSetAlarm;
Intent i;
Intent intent;
String alarmStatus;
int alarmID;
String alarmName;
String alarmTime;
String alarmAMPM;
String alarmSound;
String snoozingType;
Context context;

private SensorManager mSensorManager;
private Sensor mAccelerometer;
private ShakeListener mShakeDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dialog);

........ ........

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder
            .setTitle("Alarm Clock")
            .setMessage("What to do with " +  alarmName + ": " + alarmTime + " " + alarmAMPM +"?")
            .setCancelable(false)
            .setPositiveButton("Dismiss", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int id)
                {
                      alarmManager.cancel(pendingIntent);
                      i.putExtra("Status","Alarm Off");
                      i.putExtra("calledBefore",true);
                      i.putExtra("Alarm ID", alarmID);
                      i.putExtra("Alarm Name", alarmName);
                      i.putExtra("Alarm Time", alarmTime);
                      i.putExtra("Alarm AMPM", alarmAMPM);
                      i.putExtra("Alarm Sound", alarmSound);
                      i.putExtra("Snooze Type", snoozingType);
                      sendBroadcast(i);
                      dialog.dismiss();
                      finish();
                    //startActivity(startSetAlarm);
                }
            })
            .setNegativeButton("Snooze", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int id)
                {
                    alarmManager.cancel(pendingIntent);
                    i.putExtra("Status","Alarm Off");
                    i.putExtra("calledBefore",true);
                    i.putExtra("Alarm ID", alarmID);
                    i.putExtra("Alarm Name", alarmName);
                    i.putExtra("Alarm Time", alarmTime);
                    i.putExtra("Alarm AMPM", alarmAMPM);
                    i.putExtra("Alarm Sound", alarmSound);
                    i.putExtra("Snooze Type", snoozingType);
                    sendBroadcast(i);
                    Snooze();
                    dialog.dismiss();
                    finish();
                }
            });
    final AlertDialog alert = builder.create();
    alert.show();

And then I call the onDismiss method which I showed earlier. 然后,调用前面显示的onDismiss方法。 I also tried making a constructor of DialogActivity and initializing dialogactivity's object in main, to pass main's context into it however I get the error that "Dialog activity doesn't have a constructor with no parameters" If I make a default constructor, I still get an exception and it doesn't work. 我也尝试过制作DialogActivity的构造函数,并在main中初始化dialogactivity的对象,以将main的上下文传递给它,但是我收到以下错误消息:“ Dialog activity没有没有参数的构造函数”如果我使用默认构造函数,我仍然会得到一个例外,它不起作用。

UPDATE I figured it out. 更新我想通了。 I had to retrieve my list stored in shared preferences in order to get the updated one. 我必须检索存储在共享首选项中的列表,才能获取更新的列表。 The updated list was only getting called in onCreate not in onResume. 更新的列表仅在onCreate而不在onResume中被调用。 However I have another problem now. 但是我现在有另一个问题。 Which is still related to the same activity life cycle dilemma. 这仍然与同一活动的生命周期困境有关。 If my application is closed (and is not in opened apps list) and I receive the dialog notification, If I dismiss the alarm, it turns off and the dialog goes away. 如果我的应用程序已关闭(并且不在打开的应用程序列表中),并且收到了对话框通知,如果我关闭了警报,它将关闭并且对话框消失。 However I can see my app now in the recent list. 但是,我现在可以在最近的列表中看到我的应用程序。 And when I open my app from there, my dialog activity is active and it just keeps on showing the dialog and doesn't let me enter the app. 当我从那里打开应用程序时,我的对话框活动处于活动状态,并且一直在显示对话框,并且不允许我输入应用程序。 Unless I go to the app drawer and open the app from there. 除非我转到应用程序抽屉并从那里打开应用程序。 Here is a screenshot of what I mean. 这是我的意思的屏幕截图。 Dialog Problem 对话问题

You must use BroadcastReceiver or EventBus library . 您必须使用BroadcastReceiverEventBus库。 Your activity before create not to be refresh you ListView . 创建之前的活动不会刷新ListView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM