我正在尝试从列表视图中删除行。单击“删除”时,将删除该行。但是,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;
}
}
答案 0 :(得分:2)
即使您在存储库中删除它,该对象仍然位于适配器(_orders
)中存储的订单列表中。在致电NotifyDataSetChanged()
答案 1 :(得分: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();
}
}
答案 2 :(得分:0)
尝试在NotifyDataSetChanged()
个实例上调用OrderHistoryAdapter
(或类似内容)。