2017-06-13 13 views
0

リストビューのプログラムを追加して各リストビューのボタンを作成し、リストビューの新しいアイテムを作成するコードをいくつか持っています。各アイテムには、削除ボタン付きダイアログフラグメントを示すクリックイベントがあります。それは "月曜日"アダプターのために、それは2つのダイアログフラグメントと火曜日だけを示すので、火曜日は正しく動作します。見てください:なぜ2つのダイアログフラグメントが表示されますか?

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; 
      } 
     }; 
    } 

答えて

0

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; 

     } 
関連する問題