C# Android版Mono:ListView删除项目NotifyDataSet已更改
我正在尝试从listview中删除行。单击“删除”时,该行将被删除。但是,ListView无法更新。我必须单击“上一步”按钮,然后回来查看删除的项目。删除项目后,是否有方法刷新页面?这是我的密码:C# Android版Mono:ListView删除项目NotifyDataSet已更改,c#,android,for-loop,mono,xamarin.android,C#,Android,For Loop,Mono,Xamarin.android,我正在尝试从listview中删除行。单击“删除”时,该行将被删除。但是,ListView无法更新。我必须单击“上一步”按钮,然后回来查看删除的项目。删除项目后,是否有方法刷新页面?这是我的密码: public class OrderHistoryAdapter : BaseAdapter { private List<Order> _orders; private Activity _context; public OrderHistoryAdapter
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;
}
}
公共类OrderHistoryAdapter:BaseAdapter
{
私人名单(订单);;
私人活动——语境;
public OrderHistoryAdapter(活动上下文、列表顺序)
{
_上下文=上下文;
_订单=订单;
}
公共覆盖视图GetView(int位置、视图转换视图、视图组父视图)
{
变量项=_orders.ElementAt(位置);
变量视图=(convertView??
这个(
Resource.Layout.OrderHistoryDetailsRow,
父母亲
假)作为相对物;
TextView orderHistoryText=view.FindViewById(Resource.Id.orderHistoryText);
orderHistoryText.Text=((订单)项).Date.ToSortDateString();
view.FindViewById(Resource.Id.btnDeleteOrder)。单击+=委托
{
OrderRepository orderRepo=新建OrderRepository();
orderRepo.Delete(((订单)项).Id);
//项目已删除,但列表无法更新
NotifyDataSetChanged();
};
//最后返回视图
返回视图;
}
公共覆盖整数计数
{
获取{return_orders.Count();}
}
公共秩序(内部位置)
{
返回_orders.ElementAt(位置);
}
public override Java.Lang.Object GetItem(int位置)
{
返回null;
}
公共覆盖长GetItemId(int位置)
{
返回位置;
}
}
尝试在您的
OrderHistoryAdapter
实例上调用NotifyDataSetChanged()
(或类似的东西)。即使您在存储库中删除它,对象仍然位于存储在适配器中的订单列表中(\u orders
)。在调用NotifyDataSetChanged()
更新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();
}
}
private ListView lvAnuncios=null;
....
{
this.lvAnuncios=this.FindViewById(Resource.Id.MisAnuncios\u lvAnuncios);
}
私有void重载列表视图()
{
if(this.lvAnuncios.Adapter==null)
{
this.lvAnuncios.Adapter=new adAnuncio(this,Resource.Layout.FilaListViewAnuncio,csVariable.objUsr.lstAnuncios);
}
其他的
{
((BaseAdapter)this.lvAnuncios.Adapter).NotifyDataSetChanged();
}
}
谢谢!成功了。下面是正确的代码:if(_orders.Contains(item)){u orders.Remove(item);NotifyDataSetChanged();OrderRepository orderRepo=new OrderRepository();orderRepo.Delete((Order)item.Id);};