如何从 Xamarin.Android 中的 DatePickerDialog 刷新另一个 activity 的列表视图

How to refresh a list view from another activity from an DatePickerDialog in Xamarin.Android

我正在使用 Xamarin 开发一个项目 Android。在我的 activity 上,我有一个按钮列表视图:

当我单击其中一个按钮时,会出现 DatePickerDialog:

我想知道是否有办法在单击肯定按钮(保存按钮)后刷新 DatePickerDialog 下的 activity

下面是对话框中保存按钮的代码

    private async void HandlePositiveButton(object sender, DialogClickEventArgs e)
    {
        var dialog = (AlertDialog)sender;
        DateTime currentDate = Convert.ToDateTime(DateTextView.Text);
        DateTime changedDateTime = currentDate.Add(TimeSpan.Parse(Vm.CurrentTime));
        Vm.CurrentDateTime = changedDateTime;
        var success = await Vm.UpdateTimeLog();
    }

我想在 Activity 上为列表视图按钮做些什么我想为下面的列表视图适配器调用我的代码:

        Vm.ShowCurrentUser();
        buttonCollection = await Vm.ShowButtons();
        if (buttonCollection.Count > 0)
        {
            listAdapter = new CustomButtonAdapter(this, buttonCollection);
            ButtonListview.Adapter = listAdapter;

        }

我试过 onPause 和 OnResume,但在单击“保存”按钮并关闭对话框后不会调用它们。

来自基本适配器的下方按钮的点击事件代码:

        public async void OnClick(Android.Views.View v)
        {
                Task<string> asyncClock = Vm.ClockCommand();
                string results = await asyncClock;
                var dialog = ChangeDateTimeDialogFragment.NewInstance();
                dialog.Show(this.activity.FragmentManager, "dialog");

        }

我尝试在以下按钮的点击事件中执行以下代码:

                activity.Finish();
                activity.StartActivity(context.Intent);

不幸的是,这会消除对话框的显示。

所以我意识到我已经让父 activity 驻留在具有按钮列表视图的父 activity 的自定义基础适配器中。因此,在基本适配器的自定义按钮的单击事件中,我只是执行了以下操作,它更新了按钮的列表视图!

    private class ButtonClickListener : Java.Lang.Object, Android.Views.View.IOnClickListener
    {
        private Activity activity;
        private ObservableCollection<UserButtonLabel> buttonCollection = new ObservableCollection<UserButtonLabel>();
        private CustomButtonAdapter listAdapter;
        public ButtonClickListener(Activity activity)
        {
            this.activity = activity;
        }

        public async void OnClick(Android.Views.View v)
        {
            ListView ButtonListview = activity.FindViewById<ListView>(Resource.Id.ButtonListview);
            string name = (string)v.Tag;
            string text = string.Format("{0} Button Click.", name);
            Toast.MakeText(this.activity, text, ToastLength.Short).Show();
                Task<string> asyncClock = Vm.ClockCommand();
                string results = await asyncClock;
                var dialog = ChangeDateTimeDialogFragment.NewInstance();
                dialog.Show(this.activity.FragmentManager, "dialog");
                Vm.ShowCurrentUser();
                buttonCollection = await Vm.ShowButtons();
                if (buttonCollection.Count > 0)
                {
                    listAdapter = new CustomButtonAdapter(activity, buttonCollection);
                    ButtonListview.Adapter = listAdapter;
                    ((BaseAdapter)this.listAdapter).NotifyDataSetChanged();

                }
            }

    }

    private static UserTimeTrackingViewModel Vm
    {
        get
        {
            return App.Locator.UserTimeTracker;
        }
    }
}

这个按钮侦听器是私有的 class,无论如何都驻留在 Custom BaseAdapter 中。