How to refresh the data on a page when the backing data changes

WillAutioItrax 206 Reputation points
2020-12-22T18:31:35.583+00:00

In our Xamarin Forms app, we have a page with a ListView that shows, say, a summary of each Customer. Tapping on the Customer displays a page of details about them. Back on the main page we also have a button that opens up a Filter page where we can sort and filter the customer list. Lets say we select a city to filter the list by. We select the City from a ComboBox and tap OK. Now the main page appears again and displays the desired subset for the City that we requested.

Finally, we have a Clear Filter button on the main page, the purpose of which is to display all the Customers again. I can't get that to work.

The ListView is populated with an ObservableCollection. When applying a filter, the OnAppearing for the main page is executed and that ensures the data is displayed. When the filter is removed, the ObservableCollection is repopulated.

How do I tell the main page that it needs to refresh its data. I thought an ObservableCollection took care of that.
Thanks!

Developer technologies | .NET | Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2020-12-23T03:26:31.537+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    If the ItemsSource of your listview is like this : ObservableCollection<ItemModel> datalist = new ObservableCollection<ItemModel>(); , When you change the data of the ItemsSource(add or remove the item of listview), the listview will refresh the UI automatically.

    Finally, we have a Clear Filter button on the main page, the purpose of which is to display all the Customers again. I can't get that to work.

    For this, you can make a backup of your original data for exmaple, you can create another variable datalist_copy

    ObservableCollection<ItemModel> datalist_copy = new ObservableCollection<ItemModel>();  
    

    When you change(remove or add) the ItemsSource of your listview (datalist), when you remove or add the item of the datalist, the listview will refresh the UI automatically.

    Besides, you don't need to reload your data in method OnAppearing , you just need to add event Command for your filter Button and Clear Filter button on the main page. When you click the filter Button or Clear Filter button, you just need to change the data(remove or add) of datalist (the ItemsSource of your listview), then the listview will refresh the UI automatically.

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.