2016-08-19 73 views
0

我無法從列表中刪除項目。從列表底部刪除一個項目後,適配器顯示錯誤。從Xamarin中的列表行中刪除項目時出錯Android

我在這一行

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

項目

列表顯示圖片1

當我按下刪除鍵芝士項目在畫面2

成功刪除節目,但是當我按下了刪除奶酪&番茄刪除按鈕顯示的圖3中出現錯誤

回答

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()); 

      }; 
+0

@Thanks NightFury其工作。 –

+0

不客氣:) – NightFury

+0

我已經將提到的問題源代碼轉換爲recyclerview,我又得到了同樣的錯誤。您的答案不適用於recyclerview。 –