作者: | 来源:互联网 | 2023-07-12 18:28
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 个解决方案