[英]How to delete selected item in a ListView android xamarin
How to delete selected items in a ListView,The Items of ListView are from SQLite.net Database below is my code but when i delete all row and add new item to db ,i can not delete a row from listview如何删除 ListView 中的选定项目,ListView 的项目来自 SQLite.net 数据库,下面是我的代码,但是当我删除所有行并将新项目添加到 db 时,我无法从列表视图中删除一行
private void popmenu(object sender, AdapterView.ItemClickEventArgs e)
{
var menu = new PopupMenu(this, listnames.GetChildAt(e.Position));
menu.Inflate(Resource.Layout.popup_menu);
menu.MenuItemClick += (s, A) =>
{
switch (A.Item.ItemId)
{
case Resource.Id.pop_button1:
// update stuff
string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3");
db = new SQLiteConnection(dpPath);
var data = db.Table<LoginTable>();
// Toast.MakeText(this, "row:" + (e.Position+1), ToastLength.Short).Show();
int ww =(e.Position + 1);
var data1 = data.Where(x => x.id == ww);
foreach (var item in data1)
{
db.Delete(item);
Toast.MakeText(this, "Delete Successfully", ToastLength.Short).Show();
}
break;
}
};
You can add a remove item function in the code of your adapter for example:您可以在适配器的代码中添加删除项目功能,例如:
public class MainAdapter : BaseAdapter<string>
{
private List<string> items = new List<string>();
private Activity context;
public MainAdapter(Activity context, List<string> items) : base()
{
this.context = context;
this.items = items;
}
...
public void RemoveItemAt(int position)
{
items.RemoveAt(position);
}
}
Then in the item click event:然后在项目点击事件中:
private void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
adapter.RemoveItemAt(e.Position);
adapter.NotifyDataSetChanged();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.