热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么显示2个DialogFragments?-Whyisitdisplaying2DialogFragments?

IhavesomecodethatprogramaticallyaddsListViewsandcreatebuttonsforeachlistviewtocreate

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(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();                      
                MOndayAdapter= new Adapters.Exercise(Activity, MondayExerciseList);
                ListViewMonday.Adapter = MondayAdapter;
                layout.AddView(ListViewMonday);                   
                break;
            case "Tuesday":
                ListViewTuesday = new ListView(Activity);
                TuesdayExerciseList = new List();
                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();                      
                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();
                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();
                WednesdayAdapter = new Adapters.Exercise(Activity, WednesdayExerciseList);
                ListViewWednesday.Adapter = WednesdayAdapter;
                layout.AddView(ListViewWednesday);
                ListViewWednesday.ItemClick += ListViewWednesday_ItemClick;
                break;

        }

推荐阅读
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有