简体   繁体   English

如何将元素添加到微调器 Xamarin Android?

[英]How to add an element to a spinner, Xamarin Android?

I am building an android application with Xamarin.Android which used to be a Windows CE app.我正在使用 Xamarin.Android 构建一个 android 应用程序,它曾经是一个 ZAEA23489CE3AA9B6406EBB28EZ CEDA 应用程序。 I have this little piece of code that is supposed to loop through an array from a different project class and add the items to the checkbox ie spinner in Xamarin.我有一小段代码应该循环遍历来自不同项目 class 的数组,并将项目添加到复选框中,即 Xamarin 中的微调器。 Do you know how to do this the best way in Xamarin?你知道如何在 Xamarin 中以最好的方式做到这一点吗? Thanks.谢谢。

     docTypes = CommonData.ListDocTypes("E|");
        docTypes.Items.ForEach(dt =>
        {
            cbDocType.Items.Add(new ComboBoxItem { ID = dt.GetString("Code"), Text = dt.GetString("Code") + " " + dt.GetString("Name") });
        });

By using ArrayAdapter as said in microsoft docs.通过使用ArrayAdapter如微软文档中所述。

These are the two steps.这是两个步骤。

  1. Create the adapter object创建适配器 object

    var adapter = ArrayAdapter.CreateFromResource ( this, YOUR DATA, Android.Resource.Layout.SimpleSpinnerItem); var adapter = ArrayAdapter.CreateFromResource (这个,你的数据,Android.Resource.Layout.SimpleSpinnerItem);

    adapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);适配器.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);

  2. Assign it to spinner将其分配给微调器

    spinner.Adapter = adapter; spinner.Adapter = 适配器;

https://docs.microsoft.com/en-us/xamarin/android/user-interface/controls/spinner https://docs.microsoft.com/en-us/xamarin/android/user-interface/controls/spinner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM