【问题标题】:populating listview when item selected in spinner Xamarin在微调器 Xamarin 中选择项目时填充列表视图
【发布时间】:2016-07-16 16:00:57
【问题描述】:

我正在创建一个类型为多项选择(复选框)的 ListView,如下所示

 var adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItemMultipleChoice);
                    attendanceListView.Adapter = adapter;

现在我想在从微调器中选择项目时检查一些复选框

ListView 被填充为

adapter.Add("Select All");
                    foreach (var item in studentInformation)
                    {
                        adapter.Add(item.studentName.ToString());
                    }

到这里为止,一切都是完美的。但是现在从 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);
                        }
                    }

}

但它会引发异常:

System.InvalidCastException:指定的强制转换无效。

【问题讨论】:

  • 在你的 CourseSpinner_ItemSelected 方法中,你能在控制台中写下“sender.GetType()”的值吗?

标签: listview xamarin.android android-spinner android-checkbox


【解决方案1】:

经过一番努力,我可以通过使用来解决问题

ListView list = FindViewById<ListView>(Resource.Id.list);

而不是使用

ListView list = (ListView)sender;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 2019-05-10
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多