好吧,我有一个在listView中查看的项目列表。我在ListView中使用了所选项目的索引。
" list.indexof(listView.SelectedItem("。由此,我将索引存储为" ID",以备以后用来在以后在等等上重新发送信息。
当我从listView删除一个项目时,问题出现了。删除过程正常工作,但是在我删除了一个项目" list.indexof(listView.SelectedItem("之后,返回-1(直到我重新启动应用程序(,而不是将项目放置在ListView中的索引。为什么是这样?有没有办法解决?喜欢刷新整个视图之类的东西吗?
<mr:ListView x:Name="exampleListView" LongPressing="I_LongPressing">
<ListView.ItemTemplate>
<DataTemplate>
<ImageCell Text="{Binding mainSite}" TextColor="Black" Detail="{Binding link}" ImageSource="{Binding image}"></ImageCell>
</DataTemplate>
</ListView.ItemTemplate>
</mr:ListView>
Items = new List<ListViewItem>();
int itemValue = Items.IndexOf(exampleListView.SelectedItem); //this will return -1 if i delete an item from ListView aka the List (Items)
在评论中建议的列表中的项目代码!
public async void I_LongPressing(object sender, MR.Gestures.LongPressEventArgs e)
{
var result = await DisplayAlert("Delete", "Are you sure you want to delete this object?", "Delete", "Cancel");
if (result == true)
{
ListViewItem k = (ListViewItem)exampleListView.SelectedItem;
dataBase.Query<ListViewItem>(string.Format("DELETE FROM [ListViewItem] WHERE [link] = '{0}'", k.link));
int itemValue = Items.IndexOf(exampleListView.SelectedItem);
dataBase.Query<ObjectAndNote>(string.Format("DELETE FROM [ObjectAndNote] WHERE item = '{0}'", itemValue));
Update();
}
}
public async void Update()
{
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var asyncCon = new SQLiteAsyncConnection(path + "/TestDB.dc3");
exampleListView.ItemsSource = new List<ListViewItem>();
List<ListViewItem> refreshedList = await asyncCon.QueryAsync<ListViewItem>("SELECT * FROM ListViewItem");
await asyncCon.QueryAsync<ObjectAndNote>(string.Format("UPDATE ObjectAndNote SET item = 'item--'"));
if (refreshedList.Count != 0)
{
exampleListView.ItemsSource = refreshedList;
}
}
善意
每次删除时,您都会这样做以刷新数据
List<ListViewItem> refreshedList = await asyncCon.QueryAsync<ListViewItem>("SELECT * FROM ListViewItem");
这将创建的新列表新项目,是不是与先前存储在列表中的对象相同的唯一对象。
而不是使用查找对象的特定实例的IndexOf
,而是尝试通过ID查询列表或其他一些唯一元素以在列表中找到其位置。或者,只需更新现有列表以删除已删除的项目。