Xamarin loops over the contents of a list view

Michael Pleick 21 Reputation points
2021-07-14T06:15:46.207+00:00

Hallo,
I have a small app in Xamarin for Andrios and IOS in c#. A list is displayed there via a list view. Itemsource is an ObservableCollection that is used to load data from a local database (SQLite). These data can be filtered by the user using various functions. Everything is working well.
Now I want to loop through and process the data displayed in the ListView. To do this, I need the "ID" field in the list view of every displayed data record. I can't find a solution online. This is also possible in other environments such as VB.NET.

I have tried it this way and that.

ObservableCollection<Classes.jGeraete> result = new ObservableCollection<Classes.jGeraete>();
result = (ObservableCollection<Classes.jGeraete>)myList.ItemsSource;

foreach (var item in result.ToList())
{
DisplayAlert("Meldung", item.ID.ToString(), "ok");
}

Can someone help me?

Thanks Michael

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,284 questions
{count} votes

Accepted answer
  1. JarvanZhang 23,936 Reputation points
    2021-07-20T05:34:00.057+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    New or changed orders are marked as unread. With the loop I want to achieve that only the displayed orders can be marked as read.

    You could create a bool property in the model class to mark the item as read. And try to create two ObservableCollection<TestModel> in the viewModel class. One is used to save the data that is read from database, and the other is the 'read' data which will be shown in the listView.

    Check the code:

       public class TestViewModel  
       {  
           public ObservableCollection<TestModel> DataCollection { get; set; } //store the data of database  
           public ObservableCollection<TestModel> DisplayDataCollection { get; set; } //filter the data and display the data in the listview  
         
           public TestViewModel()  
           {  
               DataCollection = new ObservableCollection<TestModel>();  
               DisplayDataCollection = new ObservableCollection<TestModel>();  
               DataCollection.CollectionChanged += DataCollection_CollectionChanged;  
               //add data  
           }  
         
           //this event will be called when you add or remove items from the data collection  
           private void DataCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)  
           {  
               foreach (TestModel item in e.NewItems)  
               {  
                   item.PropertyChanged += Item_PropertyChanged;  
               }  
         
               DisplayDataCollection.Clear();  
               foreach (var item in DataCollection)  
               {  
                   if (item.IsRead)  
                   {  
                       DisplayDataCollection.Add(item);  
                   }  
               }  
           }  
         
           //this event will be called when you modify the items of the data collection  
           private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)  
           {  
               DisplayDataCollection.Clear();  
               foreach (var item in DataCollection)  
               {  
                   if (item.IsRead)  
                   {  
                       DisplayDataCollection.Add(item);  
                   }  
               }  
           }  
       }  
    

    Set data binding for the listview.

       //page.xaml  
       <ListView ItemsSource="{Binding DisplayDataCollection}">  
           ...  
       </ListView>  
         
       //page.xaml.cs  
       public partial class TestPage : ContentPage  
       {  
           TestViewModel viewModel;  
         
           public Page3()  
           {  
               InitializeComponent();  
         
               viewModel = new TestViewModel();  
               BindingContext = viewModel;  
           }  
       }  
    

    Best Regards,

    Jarvan 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 additional answers

Sort by: Most helpful