How to Split a List into two to populate differnt listviews?

时间:2015-04-23 05:34:38

标签: c# android xamarin

I have a list that gets populated by a API but now i want to split the list into two so i can populate two different listview on the same screen how can i do this?

items = new List<TableItem> ();
items.Add (new TableItem (){ SubProfileName = "Apple" });
items.Add (new TableItem (){ SubProfileName = "Pear" });
items.Add (new TableItem (){ SubProfileName = "Shoe" });
items.Add (new TableItem (){ SubProfileName = "Cake" });
items.Add (new TableItem (){ SubProfileName = "Vodka" });
items.Add (new TableItem (){ SubProfileName = "Alize" });
.......
.......
.......
listView = FindViewById<ListView>(Resource.Id.ListViewForSubProfiles);
listView.Adapter = new SelectProfileAdapter(this, items); 

1 个答案:

答案 0 :(得分:2)

You can use the GetRange() method to get part of a List, so in your example you need to know how many items you want in each group to split them properly, but let's say you have 10 items and you want to put 5 in each ListView, you can do this:

List<TableItem> firstList = items.GetRange(0, 5);
List<TableItem> secondList = items.GetRange(5, 5);

If you want to jump ahead, you can just the list when you set the adapter:

listView.Adapter = new SelectProfileAdapter(this, items.GetRange(startIndex, numItems));