locked
Getting error after removing an item from RecyclerView on button click. RRS feed

  • Question

  • User310286 posted

    When i remove an item at start it works well, but after removing some items, sometimes i get an error, The error is in NotifyItemRemoved(position); mAddressList.RemoveAt(position); NotifyDataSetChanged(); but i dont know how to get rid of this. Please help, here is my code.....

        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;
        using Android.Support.V7.App;
        using Android.Support.V7.Widget;
        using System.Threading.Tasks;
        using System.Data.SqlClient;
        using System.Data;
    
        namespace AlankIce
        {
            [Activity(Label = "Address")]
            public class Address : AppCompatActivity
            {
                RecyclerView mRecyclerView;
                RecyclerView.LayoutManager mLayoutManager;
                List<AddressList> mAddressList;
                AddressViewAdapter mAddressViewAdapter;
                Android.App.ProgressDialog progress;
                Class.ConAddr conString = new Class.ConAddr();
                public Button btnAddAddress;
    
                string Email;
                string Mobile;
    
                protected override void OnCreate(Bundle savedInstanceState)
                {
                    base.OnCreate(savedInstanceState);
                    SetContentView(Resource.Layout.Address);
                    //Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
                    mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
    
                    ISharedPreferences prefs = Application.Context.GetSharedPreferences("UserInfo", FileCreationMode.Private);
                    Email = prefs.GetString("Email", string.Empty);
                    Mobile = prefs.GetString("Mobile", string.Empty);
                    btnAddAddress = FindViewById<Button>(Resource.Id.btnAddAddress);
                    btnAddAddress.Click += BtnAddAddress_Click;
    
                }
    
                protected async override void OnResume()
                {
                    base.OnResume();
                    progress = new ProgressDialog(this);
                    progress.SetProgressStyle(ProgressDialogStyle.Spinner);
                    progress.SetMessage("Loading . . .");
                    progress.Show();
    
                    mAddressList = await getAddressList();
                    if (mAddressList.Count > 0)
                    {
                        mAddressViewAdapter = new AddressViewAdapter(this, mAddressList);
                        mLayoutManager = new LinearLayoutManager(this);
                        mRecyclerView.SetLayoutManager(mLayoutManager);
                        mRecyclerView.SetAdapter(mAddressViewAdapter);
                        mAddressViewAdapter.ItemClick += MAddressViewAdapter_ItemClick;
                    }
                    progress.Dismiss();
                }
    
                private void MAddressViewAdapter_ItemClick(object sender, int e)
                {
    
                }
    
                private async Task<List<AddressList>> getAddressList()
                {
                    List<AddressList> addresslist = new List<AddressList>();
                    await Task.Run(() =>
                    {
                    string qry = "select * from Address where RegEmail='"+Email+"'";
                    using (SqlConnection con = new SqlConnection(conString.conStr()))
                    {
                        DataTable dt = new DataTable();
                        using (SqlDataAdapter da = new SqlDataAdapter(qry,con))
                        {                   
                                try
                                {
                                    da.Fill(dt);
                                }
                                catch { Exception ex; }                  
    
                            if (dt.Rows.Count>0)
                            {
                                foreach(DataRow dr in dt.Rows)
                                {
                                    addresslist.Add(new AddressList { ID = dr["ID"].ToString(), Name = dr["Name"].ToString(), Mobile = dr["Mobile"].ToString(), Address = dr["Address"].ToString(), Landmark = dr["Landmark"].ToString(), State = dr["State"].ToString(), Pincode = dr["Pincode"].ToString(), City=dr["City"].ToString() });
                                }
                            }
                        }
                    }
                    });
                    return addresslist;
                }
    
                private void BtnAddAddress_Click(object sender, EventArgs e)
                {
                    Intent intent = new Intent(this,typeof(AddAddress));
                    StartActivity(intent);
                }
    
                public class AddressViewHolder : RecyclerView.ViewHolder
                {
                    Class.ConAddr conString = new Class.ConAddr();
                    public TextView tvID { get; set; }
                    public TextView tvHead { get; set; }
                    public TextView tvDetail { get; set; }
                    public TextView tvMobile { get; set; }
                    public Button btRemove { get; set; }
                    public AddressViewHolder(View itemView, Action<int> listner) : base(itemView)
                    {
                        tvID = itemView.FindViewById<TextView>(Resource.Id.tvID);
                        tvHead = itemView.FindViewById<TextView>(Resource.Id.tvHead);
                        tvDetail = itemView.FindViewById<TextView>(Resource.Id.tvDetail);
                        tvMobile = itemView.FindViewById<TextView>(Resource.Id.tvMobile);
                        btRemove = itemView.FindViewById<Button>(Resource.Id.btRemove);
                        itemView.Click += (sender, e) => listner(LayoutPosition);
                    }
                }
    
                public class AddressViewAdapter : RecyclerView.Adapter
                {
                    public event EventHandler<int> ItemClick;
                    Activity Context;
                    public List<AddressList> mAddressList;
                    Class.ConAddr conString = new Class.ConAddr();
                    public AddressViewAdapter(Activity context, List<AddressList> addressList)
                    {
                        mAddressList = addressList;
                        this.Context = context;
                    }
    
                    public override int ItemCount
                    {
                        get{ return mAddressList.Count();  }
                    }
    
                    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
                    {
                        AddressViewHolder vh = holder as AddressViewHolder;
                        vh.tvID.Text = mAddressList[position].ID;
                        vh.tvHead.Text = mAddressList[position].Name;
                        vh.tvDetail.Text = mAddressList[position].Address +", " + mAddressList[position].City + " - " + mAddressList[position].Pincode +", "+ mAddressList[position].Landmark +", " + mAddressList[position].State;
                        vh.tvMobile.Text = mAddressList[position].Mobile;
    
                        vh.btRemove.Click += async delegate
                          {
                              string qry = "delete from Address where ID ='" + vh.tvID.Text + "'";
                              using (SqlConnection con = new SqlConnection(conString.conStr()))
                              {
                                  using (SqlCommand cmd = new SqlCommand(qry, con))
                                  {
                                      await Task.Run(() =>
                                     {
                                         con.OpenAsync();
                                         cmd.ExecuteNonQuery();
                                         con.Close();
    
                                     });
                                      NotifyItemRemoved(position);
                                      mAddressList.RemoveAt(position);
                                      NotifyDataSetChanged();
                                  }
                              }
                          };
                    }
    
    
    
                    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
                    {
                        View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.AddressView, parent, false);
                        AddressViewHolder vh = new AddressViewHolder(itemView, OnClick);
                        return vh;
                    }
    
                    private void OnClick(int position)
                    {
                        if (ItemClick != null)
                            ItemClick(this, position);
                    }
                }
            }
        }
    
    Thursday, April 13, 2017 9:15 AM

All replies

  • User377044 posted

    I have the same problem, some solution?

    Friday, August 31, 2018 1:17 PM