从Xamarin Android中的列表行中删除项目时出错

时间:2016-08-19 06:19:03

标签: android xamarin

我无法从列表中删除该项目。从列表底部删除一个项目后,适配器显示错误。

我在这一行中收到错误

items.RemoveAt(position);

这是我的适配器源代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Qbill
{
    public class SelectedItemAdapter : BaseAdapter<MainActivity.Item>
    {
        List<MainActivity.Item> items;
        Activity context;

        public SelectedItemAdapter(Activity context, List<MainActivity.Item> items)
            : base()
        {
            this.context = context;
            this.items = items;

        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override MainActivity.Item 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];
            Console.WriteLine(position);
            MyViewHolder holder;

            if (convertView == null)
            {
                convertView = context.LayoutInflater.Inflate(Resource.Layout.SelectedItemListView, null);
                holder = new MyViewHolder();
                holder.Name = convertView.FindViewById<TextView>(Resource.Id.txtSelectedItemName);
                holder.Rate = convertView.FindViewById<TextView>(Resource.Id.txtSelectedItemRate);
                holder.btnAdd = convertView.FindViewById<Button>(Resource.Id.btnAdd);
                holder.btnMinus = convertView.FindViewById<Button>(Resource.Id.btnMinus);
                holder.Qty = convertView.FindViewById<TextView>(Resource.Id.txtSelectedItemQty);
                holder.btnDeleteItem = convertView.FindViewById<Button>(Resource.Id.btnDeleteItem);
                convertView.Tag = holder;
            }
            else
            {
                holder = convertView.Tag as MyViewHolder;
            }

            holder.Name.Text = item.item_name;
            holder.Rate.Text = item.item_rate;
            holder.Qty.Text = item.item_qty.ToString();
            holder.quantity = item.item_qty;

            holder.btnDeleteItem.Click += (object sender, EventArgs e) =>
            {
                int pos = (int)(((Button)sender).GetTag(Resource.Id.btnDeleteItem));
                items.RemoveAt(position);
                context.RunOnUiThread(() => this.NotifyDataSetChanged());

            };

            holder.btnAdd.Click += (o, e) =>
            {
                holder.quantity++;
                holder.Qty.Text = holder.quantity.ToString();
            };

            holder.btnMinus.Click += (o, e) =>
            {
                if (holder.quantity > 0)
                {
                    holder.quantity--;
                    holder.Qty.Text = holder.quantity.ToString();
                }
            };

           return convertView;
        }

        private class MyViewHolder : Java.Lang.Object
        {
            public TextView Qty { get; set; }
            public Button btnAdd { get; set; }
            public Button btnMinus { get; set; }
            public TextView Name { get; set; }
            public TextView Rate { get; set; }
            public int quantity { get; set; }
            public Button btnDeleteItem { get; set; }
        }
    }
}

List of item shows like this

When I press Delete button Paneer Item Successfully deleted

But When I press Delete button for delete Cheese & Tomato show an error

项目清单显示图片1

当我按下删除按钮时Paneer项目已成功删除,如图2所示

但是当我按删除按钮删除奶酪&amp;番茄在图片3中显示错误显示

1 个答案:

答案 0 :(得分:1)

由于您使用tags(index)删除项目,因此引发异常。而是将item指定为Tag到您的按钮。单击,获取项​​目对象,从数组中删除并更新列表。

将标记设置为:

holder.btnDeleteItem.SetTag("Object", item);

从列表中删除项目:

holder.btnDeleteItem.Click += (object sender, EventArgs e) =>
            {
               Item item = (Item) btn.GetTag("Object");
               items.Remove(item);
               context.RunOnUiThread(() => this.NotifyDataSetChanged());

            };