Xamarin Android - 从 ListView 中删除项目会删除两个项目
Xamarin Android - deleting item from ListView deletes TWO items
我有一个项目使用 ListView 来列出一些报价。 ListView 使用下面的适配器填充。
我最近向 ListView 项目添加了一个删除按钮,显然应该从列表中删除该项目。一切都很好。但是,在我成功删除第一个项目后,下次我单击其中一个 ListView 项目上的删除按钮时,它实际上从列表中删除了两个项目。
deleteButton.click 事件似乎触发了两次,这是我第二次尝试从列表中删除项目。
你可以看到我一直在尝试各种方法来尝试通过我拥有的各种注释代码行让它工作。
有人可以帮忙吗?
public class OffersAdapter : BaseAdapter<TableItem>
{
Button DeleteButton;
List<TableItem> items;
Activity context;
public OffersAdapter(Activity context, List<TableItem> items)
: base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override TableItem this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null)
{
view = context.LayoutInflater.Inflate (Resource.Layout.OffersLayoutItem, null);
}
// else
// {
// DeleteButton = view.FindViewById<Button> (Resource.Id.offerDeleteButton);
// //DeleteButton.SetTag (Resource.Id.deleteButton, position);
//
// }
view.FindViewById<TextView>(Resource.Id.Text1).Text = item.company;
view.FindViewById<TextView>(Resource.Id.Text2).Text = item.Heading;
view.FindViewById<TextView>(Resource.Id.Text3).Text = item.SubHeading;
view.FindViewById<ImageView> (Resource.Id.Image).SetImageBitmap (item.bitmapImage);
DeleteButton = view.FindViewById<Button> (Resource.Id.offerDeleteButton);
//DeleteButton.Tag = position;
//DeleteButton.SetTag (Resource.Id.deleteButton, position);
DeleteButton.Click += (object sender, EventArgs e) =>
{
DeleteButton.Tag = position;
Console.WriteLine("position clicked was " + position);
//int delPos = (int)(((Button)sender).GetTag (Resource.Id.offerDeleteButton));
items.RemoveAt (position);
string heading = item.Heading;
string subheading = item.SubHeading;
deleteItemFromParse(heading, subheading);
NotifyDataSetChanged ();
};
return view;
}
这最终奏效了。但我感谢您的评论和帮助。
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null)
{
view = context.LayoutInflater.Inflate (Resource.Layout.OffersLayoutItem, null);
offerDeleteButton = view.FindViewById<Button> (Resource.Id.offerDeleteButton);
offerDeleteButton.Click += (object sender, EventArgs e) =>
{
int delPos = (int)(((Button)sender).GetTag (Resource.Id.offerDeleteButton));
items.RemoveAt (delPos);
string heading = item.Heading;
string subheading = item.SubHeading;
deleteItemFromParse(heading, subheading);
NotifyDataSetChanged ();
};
}
else
{
offerDeleteButton = view.FindViewById<Button> (Resource.Id.offerDeleteButton);
}
offerDeleteButton.SetTag (Resource.Id.offerDeleteButton, position);
view.FindViewById<TextView>(Resource.Id.Text1).Text = item.company;
view.FindViewById<TextView>(Resource.Id.Text2).Text = item.Heading;
view.FindViewById<TextView>(Resource.Id.Text3).Text = item.SubHeading;
view.FindViewById<ImageView> (Resource.Id.Image).SetImageBitmap (item.bitmapImage);
return view;
}
我有一个项目使用 ListView 来列出一些报价。 ListView 使用下面的适配器填充。
我最近向 ListView 项目添加了一个删除按钮,显然应该从列表中删除该项目。一切都很好。但是,在我成功删除第一个项目后,下次我单击其中一个 ListView 项目上的删除按钮时,它实际上从列表中删除了两个项目。
deleteButton.click 事件似乎触发了两次,这是我第二次尝试从列表中删除项目。
你可以看到我一直在尝试各种方法来尝试通过我拥有的各种注释代码行让它工作。
有人可以帮忙吗?
public class OffersAdapter : BaseAdapter<TableItem>
{
Button DeleteButton;
List<TableItem> items;
Activity context;
public OffersAdapter(Activity context, List<TableItem> items)
: base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override TableItem this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null)
{
view = context.LayoutInflater.Inflate (Resource.Layout.OffersLayoutItem, null);
}
// else
// {
// DeleteButton = view.FindViewById<Button> (Resource.Id.offerDeleteButton);
// //DeleteButton.SetTag (Resource.Id.deleteButton, position);
//
// }
view.FindViewById<TextView>(Resource.Id.Text1).Text = item.company;
view.FindViewById<TextView>(Resource.Id.Text2).Text = item.Heading;
view.FindViewById<TextView>(Resource.Id.Text3).Text = item.SubHeading;
view.FindViewById<ImageView> (Resource.Id.Image).SetImageBitmap (item.bitmapImage);
DeleteButton = view.FindViewById<Button> (Resource.Id.offerDeleteButton);
//DeleteButton.Tag = position;
//DeleteButton.SetTag (Resource.Id.deleteButton, position);
DeleteButton.Click += (object sender, EventArgs e) =>
{
DeleteButton.Tag = position;
Console.WriteLine("position clicked was " + position);
//int delPos = (int)(((Button)sender).GetTag (Resource.Id.offerDeleteButton));
items.RemoveAt (position);
string heading = item.Heading;
string subheading = item.SubHeading;
deleteItemFromParse(heading, subheading);
NotifyDataSetChanged ();
};
return view;
}
这最终奏效了。但我感谢您的评论和帮助。
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null)
{
view = context.LayoutInflater.Inflate (Resource.Layout.OffersLayoutItem, null);
offerDeleteButton = view.FindViewById<Button> (Resource.Id.offerDeleteButton);
offerDeleteButton.Click += (object sender, EventArgs e) =>
{
int delPos = (int)(((Button)sender).GetTag (Resource.Id.offerDeleteButton));
items.RemoveAt (delPos);
string heading = item.Heading;
string subheading = item.SubHeading;
deleteItemFromParse(heading, subheading);
NotifyDataSetChanged ();
};
}
else
{
offerDeleteButton = view.FindViewById<Button> (Resource.Id.offerDeleteButton);
}
offerDeleteButton.SetTag (Resource.Id.offerDeleteButton, position);
view.FindViewById<TextView>(Resource.Id.Text1).Text = item.company;
view.FindViewById<TextView>(Resource.Id.Text2).Text = item.Heading;
view.FindViewById<TextView>(Resource.Id.Text3).Text = item.SubHeading;
view.FindViewById<ImageView> (Resource.Id.Image).SetImageBitmap (item.bitmapImage);
return view;
}