How to receive checked items from ListView? - c#

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'
}

Related

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;

ListView selected items add other ListView

How to add ListView selected items in other ListView?
Example project:
ExpTreeLib
Put this form ListView2 and add lv.selectedItems.
Thanks in advance.
You can use this method to add the items
ListView list = new ListView();
ListViewItem item = new ListViewItem();
// add them
list.Items.Add(item);
This would be enough, now you can use a simple loop for each element to add it to that list.
foreach (ListViewItem item in ListView.SelectedItems) {
// create a new list, or use currently present list
// add each item to it using .Add() method.
}

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();

ListViewItem: how to change List datas to ListViewItem?

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;

Fill a SharePoint List with a list<splistitem>

I'm working in a webpart. I filter (with caml) a SharePoint list and put my results in a List<SPListItem>.
Now, i need to populate another SharePoint List (I created that list in the same code) and i can't find the way to do that.
List<SPListItem> results = new List<SPListItem>() //results have the result of my query
.
.
.
SPList listFiltered = mySite.Lists[newListName]; //listFiltered is my newlist
SPListItemCollection newListItems = listFiltered.Items; //newListItem are the item from my list
foreach (SPListItem item in results)
{
//I don't know how to send my result to my SharePoint list :(
}
You will need to define your other List, then you can add a new SPListItem to that list with the columns that list contains. I am not sure what results is, if that is a typo or not, but I included that in my answer. You would need to change that if results does not exist.
SPList secondList = web.Lists["MyList"];
foreach(SPListItem item in results)
{
SPListItem Item = secondList.Items.Add();
item["Title"] = companyName
item["DateReceived"] = System.DateTime.Now;
item["Description"] = companyDesc;
item.Update();
}

Categories