【问题标题】:How to delete an Item from Sqlite & List from Xamarin如何从 Xamarin 的 Sqlite 和列表中删除项目
【发布时间】:2018-09-18 09:31:28
【问题描述】:

我遇到了一个大问题。我在 Sqlite 中有一个列表,我想通过 menuitem 单击事件删除一个项目。代码:

 async void MenuItem_Clicked(object sender, EventArgs e)
        {
            var nahrungsmittel = _nahrungsmittel[0];
            await _connection.DeleteAsync(nahrungsmittel);
            _nahrungsmittel.Remove(nahrungsmittel);

        }

目前它只删除可观察集合和数据库中的第一个对象。我怎样才能删除我点击的那个?如果这还不够,还有一些代码:

namespace EssenBestellen.Speisen
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SpeisenUebersicht : ContentPage
    {

    private SQLiteAsyncConnection _connection;
    private ObservableCollection<Nahrungsmittel> _nahrungsmittel;

    public SpeisenUebersicht()
    {
        BindingContext = new 
EssenBestellen.ViewModels.NahrungsmittelListeViewModel(new 
EssenBestellen.ViewModels.PageService());
        InitializeComponent();

        _connection = DependencyService.Get<ISQLiteDb>().GetConnection();

    }

     async protected override void OnAppearing()
     {
         await _connection.CreateTableAsync<Nahrungsmittel>();

         var nahrungsmittel = await _connection.QueryAsync<Nahrungsmittel> 
("SELECT * FROM Nahrungsmittel ORDER BY NAME ASC"); //.ToListAsync();
         _nahrungsmittel = new ObservableCollection<Nahrungsmittel> 
  (nahrungsmittel);
        speisenListe.ItemsSource = _nahrungsmittel;


        base.OnAppearing();
    }

    async void Add_Clicked(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new SpeisenAdd());
    }

    async void MenuItem_Clicked(object sender, EventArgs e)
    {
        var nahrungsmittel = _nahrungsmittel[0];
        await _connection.DeleteAsync(nahrungsmittel);
        _nahrungsmittel.Remove(nahrungsmittel);

    }


    private void speisenListe_ItemSelected(object sender, 
SelectedItemChangedEventArgs e)
    {

    (BindingContext as 






EssenBestellen.ViewModels.NahrungsmittelListeViewModel).SelectNahrungsmittel(e.S 



  electedItem as Nahrungsmittel);

    }

    private void MenuItem_Clicked_1(object sender, EventArgs e)
    {
        DisplayAlert("Kommt Noch","noch nicht implementiert", "OK");

    }


}
}

Xaml 文件更新

<ContentPage.ToolbarItems>
<ToolbarItem Icon="plus.png" Text="Neu" Activated="Add_Clicked"/>
</ContentPage.ToolbarItems>

<StackLayout>

<ListView x:Name="speisenListe" HasUnevenRows="True" ItemsSource="{Binding Nahrungsmittels}" SelectedItem="{Binding SelectedNahrungsmittel, Mode=TwoWay}" ItemTapped="speisenListe_ItemTapped" ItemSelected="speisenListe_ItemSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.ContextActions>
                    <MenuItem Text="Bearbeiten" IsDestructive="False" Clicked="MenuItem_Clicked"  Command="{Binding .}" />
                    <MenuItem Text="Löschen" IsDestructive="True" Clicked="MenuItem_Clicked"  Command="{Binding .}" />
                </ViewCell.ContextActions>
                <StackLayout Orientation="Horizontal" Padding="5">
                    <Label Text="{Binding Id}" HorizontalOptions="StartAndExpand"/>
                    <Label Text="{Binding Name}" TextColor="Gray" HorizontalOptions="CenterAndExpand"/>
                    <Label Text="{Binding Preis}" TextColor="Gray" HorizontalOptions="EndAndExpand"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【问题讨论】:

  • 您使用的是列表视图吗?我建议您也添加 XAML 代码,以便我们知道您到目前为止到底做了什么
  • 当然,我会回答我的问题并添加它
  • 不要将其添加为答案,请使用现有问题作为编辑,请参阅标签下方的“编辑”选项
  • sry 2 晚了。你能看到吗?
  • 所以您想知道您的列表视图中的哪个项目被点击了对吗?是的,你应该删除你的答案,我在问题本身中更新了它

标签: c# sqlite xamarin xamarin.forms


【解决方案1】:

在您的 ListView Itemtapped 中,您可以使用以下方法获取被点击的项目,如下所示:

 void speisenListe_ItemTapped(object sender, ItemTappedEventArgs e) 
        {
           var clickedObj= e.Item as Nahrungsmittel;
           await _connection.DeleteAsync(clickedObj);
          _nahrungsmittel.Remove(clickedObj);

        };

【讨论】:

  • 嘿,所以我不知道如何使用它。只是一个例子:我想这样做: var nahrungsmittel = _nahrungsmittel[>has du be int
  • 所以你得到了你点击的项目,但你不知道如何使用那个对象是吗?我已经编辑了我的答案,请查看
  • 好的,完美,现在我想我们都知道问题出在哪里了,谢谢。还有一个小错误:我做了这个“向左滑动(ios)或按住(droid)” - 菜单项。因此,当我从列表中保存一个项目时,会出现一个“löschen(=delete)”按钮。现在它就像:点击该项目,它就消失了。但我想用“menueItem_clicked”方法来做到这一点。问题:它不知道“e.Item”..你明白我的意思吗?
  • 我试过这样: async void MenuItem_Clicked(object sender, EventArgs e) { var clickedObj = sender as Nahrungsmittel;等待 _connection.DeleteAsync(clickedObj); _nahrungsmittel.Remove(clickedObj);但有 n 超时异常,因为我认为它想知道女巫索引哦它必须删除的项目(如“[0]”)
  • 嗯,好吧,在你的方法的第一行添加一个断点,然后检查 e 对象它得到了什么数据
【解决方案2】:

我的一个朋友帮助了我,这是解决方案:

    async void MenuItem_Clicked(object sender, EventArgs e)
    {
        if (_nm != null)
        {
            await _connection.DeleteAsync(_nm);
            _nahrungsmittel.Remove(_nm);
        }

    }


    private void speisenListe_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
       if (e.SelectedItem is Nahrungsmittel nm)
        {
            _nm = nm;
        }
    (BindingContext as EssenBestellen.ViewModels.NahrungsmittelListeViewModel).SelectNahrungsmittel(e.SelectedItem as Nahrungsmittel);

    }

还有一个小错误:您必须点击 listItem 一次,然后您可以按住/滑动并删除它。与 itemTapped 相同的问题,插入 Itemselected ......但是,现在一切都在工作

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 2019-10-02
    相关资源
    最近更新 更多