ListViewItem: how to change List datas to ListViewItem? - c#

I got 3 List (L1,L2,L3) and i want to show them on ListView.But i cant add or convert, List data's to ListViewItem.Something like this
List<string> for96 = new List<string>();
List<string> for97 = new List<string>();
List<string> for98 = new List<string>();
ListView lv1=new ListView();
ListViewItem lvitem = new ListViewItem();
lvitem.Text=for96;
lvitem.items.add(97);
lvitem.items.add(98);
lv1.items.add(lvitem);

You could take a variety of routes with this, but here's one possibility.
Join the three lists of strings together and then set that as the data source for your ListView:
var combinedLists = for96.Union(for97).Union(for98).ToList();
listView.ItemsSource = combinedLists;

Related

How to receive checked items from ListView?

I have a List<> of items which are stored in a ListView with CheckBox'es. What I need is to store checked items to another List<>. Here is the code how the ListView is displayed and populated with data:
List<Product> _productsList = ProductsFromXml();
List<Product> checkedProducts = new List<Product>();
productsListView = FindViewById<ListView>(Resource.Id.listView1);
productsListView.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItemMultipleChoice, _productsList);
productsListView.ChoiceMode = ChoiceMode.Multiple;
Ok. Below is the c# code:
foreach (ListViewItem xitem in productsListView.CheckedItems)
{
// Do whatever you want with checked item 'xitem'
}

How to convert string list to observable collection in windows phone

Hi all i have a list string like
List<string> numbers=new List<string>();
now i want to convert it on observable collection i have converted it successfully like
ObservableCollection<string> myCollection = new ObservableCollection<string>(numbers);
but when i am deleting a item from list box like
myCollection.Remove(listBox1.SelectedItem.ToString());
listBox1.ItemsSource = myCollection;
the above code is deleted all the item in list box but i want to delete specific item in list box.
Try this
Initialize collection and lsitbox
List<string> numbers=new List<string>();
//numbers.Add("test"); //populate list
ObservableCollection<string> myCollection = new ObservableCollection<string>(numbers);
listBox1.ItemsSource = myCollection;
now use the below code to remove the selected item from the list
var selectedItem =listbox1.SelectedItem as string;
if(myCollection.Contains(selectedItem)
{
myCollection.Remove(selectedItem);
}
Instead of binding ObservableCollection you can directly bind List<string> to your listBox1.ItemsSource. See this example to bind List<string>
windows phone data binding listpicker to List of Strings
To remove items from list try this
listBox1.Items.Remove(listBox1.SelectedItem);
Answer_:
numbers.RemoveAt(listBox1.SelectedIndex);
listBox1.ItemsSource = null;
listBox1.ItemsSource = numbers;

how to Copy listView selected items into an array

I'm trying to get listView items into an array..
in listBox
listBox1.SelectedItems
would do the trick.
But it didn't work in listView...
any Ideas???
Do like this,
var myList = new List<string>();
foreach(ListViewItem Item in ListView.SelectedItems)
{
myList.add(Item.Text.ToString());
}
var myArray = myList.ToArray();

What's the best way to add a copy of a list in another list?

I tried to add naively a list in another but I loose the data when I clear this first list.
List<List<string>> lls = new List<List<string>>();
List<string> ls = new List<string>();
ls.Add("a");
ls.Add("b");
ls.Add("c");
lls.Add(ls);
ls.Clear();
foreach (List<string> lst in lls)
foreach (string s in lst)
System.Diagnostics.Debug.WriteLine(s); // Display nothing
So I tried to "copy" my List<string> in the other list but I don't really know how to simply and properly do this. What's the best way to copy data in a list ?
You need to create a new list with the same items:
new List<string>(ls)
List<string> ls = new List<string>(lls);

How to fill a list view with the contents of List<string> in C#

I've a list
List<String> SampleList=new List<String>();
I need to fill a listView with the contents of the list
For example the "SampleList" contains
a
b
c
d
The listView should be filled like
S.No Item
1 a
2 b
3 c
4 d
Now i'm using for loop for this method
like
for(int i=0;i<SampleList.Count;i++)
{
listView1.Items.Add((i+1).ToString());
listView1.Items[i].SubItems.Add(SampleList[i]);
}
is there any other way to do this like data binding ?
Thanks in advance
Not quite like databinding, but you could use VirtualMode and RetrieveVirtualItem
listView1.VirtualMode = true;
listView1.RetreiveVirtualItem += new RetrieveVirtualItemEventHandler( this.RetrieveVirtualItem );
listView1.VirtualListSize = SampleList.Count;
private void RetreiveVirtualItem( object sender, RetrieveVirtualItemEventArgs e )
{
ListViewItem lvItem = new ListViewItem((e.ItemIndex + 1).ToString());
lvItem.SubItems.Add(SampleList[e.ItemIndex]);
e.Item = lvItem;
}
Does it have to be a ListView? ListBox is simple:
using (Form form = new Form())
{
List<string> strings = new List<string> {"abc", "def", "ghi"};
form.Controls.Add(new ListBox() {DataSource = strings});
Application.Run(form);
}
For a richer display, DataGridView would also do this, but you need an extra bit of indirection (since it supports multiple columns, it needs a wrapper object per row):
using (Form form = new Form())
{
List<string> strings = new List<string> {"abc", "def", "ghi"};
var indirect = (from s in strings
select new {Text = s}).ToList();
form.Controls.Add(new DataGridView() { DataSource = indirect });
Application.Run(form);
}
This also gives you opportunity to add in extra data, for example the number:
var indirect = strings.Select((s,i) =>
new {Index = i + 1, Text = s}).ToList();
Unfortunately windows forms ListView doesn't support DataBinding. But if you update the list frequently, maybe you can use INotifyProperty interface.

Categories