I have a project that uses a ListView to list some offers. The ListView is populates with the adapter below.
I've recently added a delete button to the ListView items, which should obviously delete the item from the list. All is good. However, after i delete my first item successfully, the next time i click on a delete button on one of the ListView items, it actually deletes TWO items from the list.
The deleteButton.click event seems to trigger twice, the second time i attempt to delete an item from the list.
You can see i've been playing around with various ways to try and get it working by the various commented code lines i have.
Can anybody help please?
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;
}
DeleteButton.Tag
line ? – ρяσѕρєя K