Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

how to delete an item in a ListView using SwipeView

$
0
0

Hi, this seems to be easy but not for everyone.

I can delete the item using Command (the right way for MVVM) or using code behind.

First, if I use code behind it is easy to display an alert message but I don't know how to get the item:
XAML:

                                   <SwipeItems Mode="Execute">
                                        <SwipeItem Text="{i18n:Translate buttonDelete}"
                                                IconImageSource="delete.png"
                                                BackgroundColor="{StaticResource TextRedNightColor}"
                                                Invoked="OnDelete"/>
                                    </SwipeItems>

code behind:

        async void OnDelete(object sender, EventArgs args)
        {
                bool answer = await DisplayAlert(
                rm.GetString("warning"),
                rm.GetString("messageDeleteSight"),
                rm.GetString("deleteAnyway"),
                rm.GetString("cancel"));
                if (answer)
                {
                    //await AlmicantaratXF.Views.App.PositionsSightsDB.DeleteSightAsync(***some thing to write here to get the item***);
                }
            }
        }

When I tap on an item I get the item easily:

        async void OnSightSelected(object sender, SelectedItemChangedEventArgs e)
        {
            (sender as ListView).SelectedItem = null;
            if (e.SelectedItem != null)
            {
                await Navigation.PushAsync(new SightPage(e.SelectedItem as Sight));
            }
        }

But when I swipe the item I don't have any SelectedItemChangedEventArgs e, so I don't know how to get the item.

Second, I did delete the item using Command, but there is no more alert message and I don't know how to refresh the ListView so that the item does not appear anymore.
The ListView is populated during the OnAppearing (does that help?):

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            Model.Position currentPosition = BindingContext as Model.Position;
            listView.ItemsSource = await AlmicantaratXF.Views.App.PositionsSightsDB.GetSightsAsync(currentPosition);
        }
  1. how to get the item in the code behind after a swipe?
  2. how to refresh the listview when the item is deleted?

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>