gpt4 book ai didi

c# - Android 单声道 : ListView Delete Items NotifyDataSet Changed

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:45 28 4
gpt4 key购买 nike

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

最佳答案

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

之前尝试从该列表中删除该对象

关于c# - Android 单声道 : ListView Delete Items NotifyDataSet Changed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10014699/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com