ListViews在很多应用程序中都非常有用,但修改它们对我来说有点棘手。我正在尝试创建一个自定义列表视图,每个项目上都有一个按钮,当单击该项目时,该特定项目将被删除。我尝试了下面的代码,但它总是多次触发,并且它给出了超出范围的异常。
class DataAdapter : BaseAdapter
{
private Activity activity;
private List<DummyData> dataList;
public DataAdapter(Activity activity, List<DummyData> dataList)
{
this.activity = activity;
this.dataList = dataList;
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return dataList[position].Id;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView ?? activity.LayoutInflater.Inflate(Resource.Layout.DummyDataLayout, parent, false);
TextView nameTextView = view.FindViewById<TextView>(Resource.Id.nameTextView);
Button removeButton = view.FindViewById<Button>(Resource.Id.removeButton);
nameTextView.Text = dataList[position].Name;
removeButton.Click += (object sender, EventArgs e) =>
{
dataList.Remove(dataList[position]);
NotifyDataSetChanged();
};
return view;
}
//Fill in cound here, currently 0
public override int Count
{
get
{
return dataList.Count;
}
}
}
class DataAdapterViewHolder : Java.Lang.Object
{
public TextView nameTextView { get; set; }
public Button removeButton { get; set; }
}
}