我正在尝试从列表视图中删除行。当我单击删除时,该行被删除。但是,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;
}
}