-1

I have a custom listview with checkBoxes and populates it with data from List<>. I was thinking how to receive the items which are checked and I basically couldn't find the solution. Is it possible to make it with the ListView like this or should I make a custom ListView? Any ideas? Thanks in advance.

Here is the code I am trying to deal with:

         public string content;
         ListView productsListView; 

         List<Product> _productsList = ProductsFromXml();

        //list for checked items (not yet)
        List<Product> checkedProducts = new List<Product>();

        //creating a ListView within our products
        productsListView = FindViewById<ListView>(Resource.Id.listView1);
        productsListView.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItemMultipleChoice, _productsList);
        productsListView.ChoiceMode = ChoiceMode.Multiple;  
Zilvinas
  • 19
  • 4

1 Answers1

0

Create a property on your Product object like bool Checked and set it to true/false on the ItemClick event.

Then you can go through the list and see which items are selected.

This Xamarin guide shows you how to listen to click events for a ListView backed by an ArrayAdapter.

Your Activity will need to inherit from ListActivity and then you can override

OnListItemClick (ListView l, View v, int position, long id)

and use the int position parameter to index the Products list and se the value to true/false to indicate selection.

pnavk
  • 4,552
  • 2
  • 19
  • 43