0
votes

I have a listview and what I want to do is when I click on an item, a new page is launch with the details. On this page there will be a delete button, and if I press it, the element is deleted from the database and the page is popasync to go back to the list.

Then I need to update my listview, currently I made it work thanks to a messagingcenter but I don't know if it's the best solution.

This is how I launch my detailspage :

cardList.ItemTapped += async (sender, e) =>
{
    await Navigation.PushAsync(new CardDetails((Card) e.Item, database));     
};

This is my details page with the delete code :

var toolbarItem = new ToolbarItem
{
    Name = "Supprimer",
    Command = new Command(this.deleteAndReturn),             
};

private void deleteAndReturn()
{
    database.deleteCard(card);
    Navigation.PopAsync();
    MessagingCenter.Send<CardDetails>(this, "Delete");
}

On my listPage I update the view like that :

MessagingCenter.Subscribe<CardDetails>(this, "Delete", (sender) => {
    cardList.ItemsSource = database.getCards();
});

Is it a good way to do that ? I already so that we can listen to event like "popped" but I don't know how to listen to it. I think the equivalent in android would be "OnActivityResult".

3

3 Answers

3
votes

So instead of re-setting the ItemSource or creating a custom render you can use an ObservableCollection for this behaviour.

Simply make the IEnumarbale which is the ItemSource of your ListView a ObservableCollection. Any changes to this list will be directly propagated to the ListView itself.

0
votes

it is not refresh after edit or delete list item so, after Delete event completed then again you should to get card from database and set on list-view like :

cardList.ItemsSource = database.getCards();

0
votes

You need to create a custom rendered for your List View. here i have write down the xamarin forum links please follow it. https://forums.xamarin.com/discussion/38472/refresh-listview-after-deleting-list-item there is multiple option and it is working i think that one of them is best for you.