populating listview when item selected in spinner Xamarin

I am creating a ListView of type multiple choices (check boxes) as below

var adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItemMultipleChoice);
attendanceListView.Adapter = adapter;
now I want to check some checkBoxes when item is selected from spinner

and ListView is populated as

adapter.Add("Select All");
foreach (var item in studentInformation)
{
adapter.Add(item.studentName.ToString());
}
every thing is perfect till here. but now problem arises when selecting item from courseSpinner

private void CourseSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
ListView list = (ListView)sender;
for (int count = 0; count < attendanceInformation.Count(); count++)
{
if (attendanceInformation[i].status == true)
{
selectedStudents[i] = attendanceInformation[i].studentId;
list.SetItemChecked(i, true);
}
else
{
list.SetItemChecked(i, false);
}
}

}
but it throws an exception "System.InvalidCastException: Specified cast is not valid."

Urgent help is appreciated

Tagged:

Best Answer

Answers

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    ListView list = (ListView)sender;

    is most probably the source of problems. Since CourseSpinner is a Spinner (isn't it?), not a ListView, the sender is of type Spinner, not ListView.

    Hence

    private void CourseSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
    {
    Spinner list = (Spinner)sender;
    
  • UMARFAROOQ.8478UMARFAROOQ.8478 USMember

    @MihaMarkic you are right, but when i do so as you suggested then list options are not available i.e. list.SetItemChecked().

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    Why are you using Spinner then?

Sign In or Register to comment.