Xamarin loops over the contents of a list view

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
Hallo and Thanks for your Answer.
do I understand the code correctly? a global collection is defined and this is used later in a loop?
Best Regards,
Michael
The collection is from the viewModel class and it is just the one you bind the
ItemsSource
for the listView.maybe I asked the question wrong (sorry, I'm better in German). in one procedure I set the itemsource of a ListView and in another procedure I want to execute a loop over the contents of the Listview.
I would like to describe this using an example in .NET with a DataGridView. c# is still quite new to me.
sub Procedur1()
Datagridview. Datasource = Data
end Sub
sub myLoop()
dim data as datatable
data=Datagridview.Datasource
for each r in data.rows
....
next
end Sub
or
Sub myLoop2()
for i=0 to Datagridview.Rows.Count-1
....
loop
end sub
is that better explained?
Best Regard,
Michael
Do you want to scroll the items in a loop? ListView doesn't provide a property to loop the items. A workaround is to add the items to itemsSource when scrolling to the last item.
What view do you want to display and loop? Xamarin.Forms.CarouselView provides the loop function, you could try it.
https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.carouselview.loop?view=xamarin-forms
I show an OrderList consisting of orders for Devices in the ListView. The primary key is the order number (Auftrtragsnummer) . 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.
private async void miGelesen_Clicked(object sender, EventArgs e)
{
// Übergabe des Parameters NetBiosName (Transfer of the parameter NetBiosName)
var mi = ((MenuItem)sender);
Instead of the get_ListAuftraege() function, I would like to read the data from the Listview.ItemSource. My data is already there and, if necessary, filtered the way I would like it to be.
Best regards,
Michael
Sign in to comment
Hello,
Welcome to our Microsoft Q&A platform!
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:
Set data binding for the listview.
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.
I don't understand this approach.
Probably it is not possible to execute a loop over a ListView or the ListView-Itemsource. Then I leave the code as it is now, it works.
Best Regards,
Michael
Yes, ListView doesn't provide a property to loop the items. It's hard to do that because there may many items on the screen at the same, it requires to caculate to number of the items on the screen and update the collection of the itemsSource. You could send a feature request to the product team to seek for a help on github.
Sign in to comment
0 additional answers
Sort by: Most helpful
Activity