Я создал демо для имитации вашего кода, он работает правильно.Основной код:
CheckBoxAdapter.cs
public class CheckBoxAdapter : BaseAdapter<ItemModel>
{
private Activity activity;
private List<ItemModel> datalist;
public CheckBoxAdapter(Activity activity, List<ItemModel> datalist)
{
this.activity = activity;
this.datalist = datalist;
}
public override ItemModel this[int position] {
get { return datalist[position]; }
}
public override int Count {
get { return datalist.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
MyViewHolder holder;
if (view != null)
{
holder = view.Tag as MyViewHolder;
holder.mCheckBox.Tag = position;
}
else
{
holder = new MyViewHolder();
view = this.activity.LayoutInflater.Inflate(Resource.Layout.item_layout, null);
holder.mCheckBox = view.FindViewById<CheckBox>(Resource.Id.chkCaptain);
holder.DeliveryCompany = view.FindViewById<TextView>(Resource.Id.txtName);
holder.StopId = view.FindViewById<TextView>(Resource.Id.txtTeam);
holder.mCheckBox.Tag = position;
view.Tag = holder;
}
ItemModel player = this.datalist[position];
holder.mCheckBox.SetOnCheckedChangeListener(null);
holder.mCheckBox.Checked = player.IsChecked;
holder.mCheckBox.SetOnCheckedChangeListener(new CheckedChangeListener(this.activity,datalist));
return view;
}
public class MyViewHolder : Java.Lang.Object
{
public TextView DeliveryCompany { get; set; }
public CheckBox mCheckBox { get; set; }
public TextView StopId { get; set; }
}
private class CheckedChangeListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener
{
private Activity activity;
private List<ItemModel> list;
public CheckedChangeListener(Activity activity, List<ItemModel> datalist)
{
this.activity = activity;
this.list = datalist;
}
public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
{
if (isChecked)
{
int position = (int)buttonView.Tag;
ItemModel item = list[position];
item.IsChecked = true;
list[position].IsChecked = true;
string text = string.Format("{0} Checked.", item.Name);
Toast.MakeText(this.activity, text, ToastLength.Short).Show();
}
else {
int position = (int)buttonView.Tag;
list[position].IsChecked= false;
}
}
}
}
Когда мы нажмем кнопку, мы получим все выбранные элементы списка флажков путем фильтрации списка данных:
private void MBtn_Click(object sender, System.EventArgs e)
{
foreach (ItemModel model in dataList) {
if (model.IsChecked) {
System.Diagnostics.Debug.WriteLine("selected item = " + model.Name );
}
}
}
Примечание:
1.Мы можем установить и получить позицию элемента, установив Tag
в CheckBox
.
if (view != null)
{
holder = view.Tag as MyViewHolder;
holder.mCheckBox.Tag = position;
}
else
{
holder = new MyViewHolder();
view = this.activity.LayoutInflater.Inflate(Resource.Layout.item_layout, null);
holder.mCheckBox = view.FindViewById<CheckBox>(Resource.Id.chkCaptain);
holder.DeliveryCompany = view.FindViewById<TextView>(Resource.Id.txtName);
holder.StopId = view.FindViewById<TextView>(Resource.Id.txtTeam);
holder.mCheckBox.Tag = position;
view.Tag = holder;
}
2.Переменная CheckBox
вашего ListAdapterViewHolder
неверна, это Имя класса.
class ListAdapterViewHolder : Java.Lang.Object
{
public TextView DeliveryCompany { get; set; }
public TextView StopId { get; set; }
public CheckBox CheckBox { get; set; } // here
}