4

我正在尝试从列表视图中删除行。当我单击删除时,该行被删除。但是,ListView 无法更新。我必须单击后退按钮并回来查看已删除的项目。有没有办法在删除项目后刷新页面?这是我的代码:

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

3 回答 3

2

即使您在存储库中删除它,该对象仍位于适配器 ( _orders) 中存储的订单列表中。在调用之前尝试从该列表中删除对象NotifyDataSetChanged()

于 2012-04-04T22:08:21.083 回答
1

更新 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();
        }
   }
于 2013-06-10T23:43:37.203 回答
0

尝试NotifyDataSetChanged()在您的OrderHistoryAdapter实例上调用(或类似的)。

于 2012-04-04T15:55:23.453 回答