4

I am trying to delete rows from a listview. When I click delete, the row is removed. The ListView, however, fails to update. I have to click the back button and come back to see the item removed. Is there a way to refresh the page, after an item has been removed? Here is my code:

public class OrderHistoryAdapter : BaseAdapter
{
    private List<Order> _orders;
    private Activity _context;


    public OrderHistoryAdapter(Activity context, List<Order> orders)
    {
        _context = context;
        _orders = orders;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = _orders.ElementAt(position);

        var view = (convertView ??
                this._context.LayoutInflater.Inflate(
                Resource.Layout.OrderHistoryDetailsRow,
                parent,
                false)) as RelativeLayout;




        TextView orderHistoryText = view.FindViewById<TextView>(Resource.Id.orderHistoryText);
        orderHistoryText.Text = ((Order)item).Date.ToShortDateString();
        view.FindViewById<TextView>(Resource.Id.btnDeleteOrder).Click += delegate
        {
            OrderRepository orderRepo = new OrderRepository();
            orderRepo.Delete(((Order)item).Id);
            //Item has been deleted, yet list fails to update
            NotifyDataSetChanged();
        };


        //Finally return the view
        return view;
    }


    public override int Count
    {
        get { return _orders.Count(); }
    }

    public Order GetOrder(int position)
    {
        return _orders.ElementAt(position);
    }

    public override Java.Lang.Object GetItem(int position)
    {
        return null;
    }

    public override long GetItemId(int position)
    {
        return position;
    }
}
dymmeh
  • 22,247
  • 5
  • 53
  • 60
Joseph Anderson
  • 4,114
  • 4
  • 45
  • 99

3 Answers3

2

Even though you're deleting it in the repository the object is still sitting in the list of orders stored in the adapter (_orders). Try removing the object from that list before you call NotifyDataSetChanged()

Greg Shackles
  • 10,009
  • 2
  • 29
  • 35
  • Thanks! That worked. Here is the right code: if (_orders.Contains(item)) { _orders.Remove(item); NotifyDataSetChanged(); OrderRepository orderRepo = new OrderRepository(); orderRepo.Delete(((Order)item).Id); } }; – Joseph Anderson Apr 04 '12 at 22:15
1

To update the ListView

   private ListView lvAnuncios= null;

   ....
   {
        this.lvAnuncios = this.FindViewById<ListView>(Resource.Id.MisAnuncios_lvAnuncios);
   }

   private void ReloadListView()
   {
        if (this.lvAnuncios.Adapter == null)
        {
            this.lvAnuncios.Adapter = new adAnuncio(this, Resource.Layout.FilaListViewAnuncio, csVariable.objUsr.lstAnuncios);
        }
        else
        {
            ((BaseAdapter)this.lvAnuncios.Adapter).NotifyDataSetChanged();
        }
   }
0

Try to call NotifyDataSetChanged() (or something like) on your OrderHistoryAdapter instance.

pepyakin
  • 2,217
  • 19
  • 31