Xamarin Android - 从ListView删除项目删除两个项目

时间:2015-02-12 10:20:30

标签: android listview android-listview xamarin listadapter

我有一个使用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;
        }

1 个答案:

答案 0 :(得分:0)

这最终奏效了。但我感谢你的评论和帮助。

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;
            }