0
我有一些代碼,編程添加ListViews併爲每個列表視圖創建按鈕,爲列表視圖創建新項目,並且每個項目都有一個單擊事件,顯示帶有刪除按鈕的對話框片段。它可以工作,但是對於「星期一」適配器,它顯示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;
}
};
}