简体   繁体   中英

Adding a single item to a ListView in Xamarin.iOS

I have a Xamarin.iOS application with a ListView in a .xaml file that originally populates with around 4000 items (potentially more) from a DB at startup.

<ListView x:Name="listView"></ListView>

The user will make entries in another page of the application while it's open, so I want to add a function in the .xaml.cs file to update the ListView with only that entry so the other 4000 entries don't have to be accessed again.

Setting all the items in a ListView with a string[] or List<string> is simple with listView.ItemsSource = myStringArray; ; however, I can't seem to figure out how to add a single item (string) into the ListView. Some answers include using an ArrayAdaptor<string> but this is only for Xamarin.Android. How can I add one string into a ListView in Xamarin.iOS?

ObservableCollection<string> data = new ObservableCollection<string>();

// do something to populate data with an initial dataset

MyListView.ItemsSource = data;

// to add an item to the ListView, add it to the ItemsSource 
data.Add("my new item");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM