为什么显示2个Dialog Fragments?

时间:2022-04-07 19:44:25

I have some code that programatically adds ListViews and create buttons for each list view to create new items for the list views, and each item has a click event that shows a dialog fragment with a delete button. It works, however for the "Monday" adapter, it shows 2 Dialog Fragments, and Tuesday only one, so tuesday works correctly. Take a look:

我有一些代码以编程方式添加ListView并为每个列表视图创建按钮以创建列表视图的新项目,并且每个项目都有一个单击事件,显示带有删除按钮的对话框片段。它适用于“星期一”适配器,它显示2个Dialog Fragments,而星期二只显示一个,所以星期二工作正常。看一看:

private void InitializeExercisesForDay(string day, View view)
    {
        /*
         * Basic start
         * */

        LinearLayout layout = view.FindViewById<LinearLayout>(Resource.Id.linearLayoutCreatePlan);

        TextView text = new TextView(Activity);
        text.SetTextColor(Resources.GetColor(Resource.Color.lightGreen));
        text.Text = day;
        layout.AddView(text);

        Button buttonAddExercise = new Button(Activity)
        {
            Text = "Add Exercise"
        };
        layout.AddView(buttonAddExercise);

        /*
         * Basic End */


        switch (day)
        {
            case "Monday":

                ListViewMonday = new ListView(Activity);
                MondayExerciseList = new List<Exercise>();                      
                MondayAdapter = new Adapters.Exercise(Activity, MondayExerciseList);
                ListViewMonday.Adapter = MondayAdapter;
                layout.AddView(ListViewMonday);                   
                break;
            case "Tuesday":
                ListViewTuesday = new ListView(Activity);
                TuesdayExerciseList = new List<Exercise>();
                TuesdayAdapter = new Adapters.Exercise(Activity, TuesdayExerciseList);
                ListViewTuesday.Adapter = TuesdayAdapter;
                layout.AddView(ListViewTuesday);
                break;
        }

        buttonAddExercise.Click += delegate
        {
            FragmentTransaction transaction = Activity.FragmentManager.BeginTransaction();
            Helpers.DialogViewExercises dialog = new Helpers.DialogViewExercises(Activity);
            dialog.Show(transaction, "dialog");
            dialog.OnExerciseSelected += (o, e) =>
            {
                var exercise = e.Exercise;

                switch (day)
                {
                    case "Monday":
                        MondayExerciseList.Add(exercise);
                        MondayAdapter = new Adapters.Exercise(Activity, MondayExerciseList);
                        Activity.RunOnUiThread(() => { MondayAdapter.NotifyDataSetChanged(); });
                        ListViewMonday.Adapter = MondayAdapter;
                        if (ListViewMonday != null)
                        {
                            var parameters = GetListViewParameters(ListViewMonday, MondayAdapter);
                            ListViewMonday.LayoutParameters = parameters;
                        }


                        break;

                    case "Tuesday":
                        TuesdayExerciseList.Add(exercise);
                        TuesdayAdapter = new Adapters.Exercise(Activity, TuesdayExerciseList);
                        Activity.RunOnUiThread(() => { TuesdayAdapter.NotifyDataSetChanged(); });
                        ListViewTuesday.Adapter = TuesdayAdapter;
                        if (ListViewTuesday != null)
                        {
                            var parameters = GetListViewParameters(ListViewTuesday, TuesdayAdapter);
                            ListViewTuesday.LayoutParameters = parameters;
                        }


                        break;
                }
            };
        };

        if (ListViewMonday != null)
        {
            ListViewMonday.ItemClick += ListViewMonday_ItemClick;
            return;
        }

        if (ListViewTuesday != null)
        {
            ListViewTuesday.ItemClick += ListViewTuesday_ItemClick;
            return;
        }

    }

    private void ListViewTuesday_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        var exercise = TuesdayExerciseList[e.Position];

        FragmentTransaction transaction = Activity.FragmentManager.BeginTransaction();
        Helpers.DialogViewExerciseInPlan dialog = new Helpers.DialogViewExerciseInPlan(exercise);
        dialog.Show(transaction, "dialog");
        dialog.OnDelete += delegate
        {
            TuesdayExerciseList.Remove(exercise);
            TuesdayAdapter = new Adapters.Exercise(Activity, TuesdayExerciseList);
            Activity.RunOnUiThread(() => { TuesdayAdapter.NotifyDataSetChanged(); });
            ListViewTuesday.Adapter = TuesdayAdapter;
            if (ListViewTuesday != null)
            {
                var parameters = GetListViewParameters(ListViewTuesday, TuesdayAdapter);
                ListViewTuesday.LayoutParameters = parameters;
            }
        };
    }

    private void ListViewMonday_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        var exercise = MondayExerciseList[e.Position];

        FragmentTransaction transaction = Activity.FragmentManager.BeginTransaction();
        Helpers.DialogViewExerciseInPlan dialog = new Helpers.DialogViewExerciseInPlan(exercise);
        dialog.Show(transaction, "dialog");
        dialog.OnDelete += delegate
        {
            MondayExerciseList.Remove(exercise);
            MondayAdapter = new Adapters.Exercise(Activity, MondayExerciseList);
            Activity.RunOnUiThread(() => { MondayAdapter.NotifyDataSetChanged(); });
            ListViewMonday.Adapter = MondayAdapter;
            if (ListViewMonday != null)
            {
                var parameters = GetListViewParameters(ListViewMonday, MondayAdapter);
                ListViewMonday.LayoutParameters = parameters;
            }
        };
    }

1 个解决方案

#1


0  

Figured it out. The click event had to be inside the switch statement, but I don't know why. Does anybody know?

弄清楚了。 click事件必须在switch语句中,但我不知道为什么。有人知道吗?

        switch (day)
        {
            case "Monday":                   
                ListViewMonday = new ListView(Activity);
                MondayExerciseList = new List<Exercise>();                      
                MondayAdapter = new Adapters.Exercise(Activity, MondayExerciseList);
                ListViewMonday.Adapter = MondayAdapter;
                layout.AddView(ListViewMonday);
                ListViewMonday.ItemClick += ListViewMonday_ItemClick;
                break;
            case "Tuesday":
                ListViewTuesday = new ListView(Activity);
                TuesdayExerciseList = new List<Exercise>();
                TuesdayAdapter = new Adapters.Exercise(Activity, TuesdayExerciseList);
                ListViewTuesday.Adapter = TuesdayAdapter;
                layout.AddView(ListViewTuesday);
                ListViewTuesday.ItemClick += ListViewTuesday_ItemClick;
                break;
            case "Wednesday":
                ListViewWednesday = new ListView(Activity);
                WednesdayExerciseList = new List<Exercise>();
                WednesdayAdapter = new Adapters.Exercise(Activity, WednesdayExerciseList);
                ListViewWednesday.Adapter = WednesdayAdapter;
                layout.AddView(ListViewWednesday);
                ListViewWednesday.ItemClick += ListViewWednesday_ItemClick;
                break;

        }

#1


0  

Figured it out. The click event had to be inside the switch statement, but I don't know why. Does anybody know?

弄清楚了。 click事件必须在switch语句中,但我不知道为什么。有人知道吗?

        switch (day)
        {
            case "Monday":                   
                ListViewMonday = new ListView(Activity);
                MondayExerciseList = new List<Exercise>();                      
                MondayAdapter = new Adapters.Exercise(Activity, MondayExerciseList);
                ListViewMonday.Adapter = MondayAdapter;
                layout.AddView(ListViewMonday);
                ListViewMonday.ItemClick += ListViewMonday_ItemClick;
                break;
            case "Tuesday":
                ListViewTuesday = new ListView(Activity);
                TuesdayExerciseList = new List<Exercise>();
                TuesdayAdapter = new Adapters.Exercise(Activity, TuesdayExerciseList);
                ListViewTuesday.Adapter = TuesdayAdapter;
                layout.AddView(ListViewTuesday);
                ListViewTuesday.ItemClick += ListViewTuesday_ItemClick;
                break;
            case "Wednesday":
                ListViewWednesday = new ListView(Activity);
                WednesdayExerciseList = new List<Exercise>();
                WednesdayAdapter = new Adapters.Exercise(Activity, WednesdayExerciseList);
                ListViewWednesday.Adapter = WednesdayAdapter;
                layout.AddView(ListViewWednesday);
                ListViewWednesday.ItemClick += ListViewWednesday_ItemClick;
                break;

        }