简体   繁体   English

Android Spinner没有使用新选择的项目Xamarin.Android进行更新

[英]Android Spinner not updating with newly Selected item Xamarin.Android

I have a spinner containing a list of countries. 我有一个包含国家列表的微调器。 The page load with the following 页面加载如下

在此输入图像描述

The user select a country from the drop down menu. 用户从下拉菜单中选择一个国家/地区。 Let say we select Algeria 我们说选择阿尔及利亚

在此输入图像描述

After Algeria is selected the drop down will close but the selected country is still shown as Unknown 选择阿尔及利亚后,下拉菜单将关闭,但所选国家/地区仍显示为“未知”

在此输入图像描述

Next, when I select another country, only then the previous country which was Algeria is shown. 接下来,当我选择另一个国家时,只显示前一个阿尔及利亚国家。 Not the newly selected country. 不是新选择的国家。

在此输入图像描述

My code 我的代码

public class SpinnerActivity : Activity
{
    private ArrayAdapter adapter;
    private Spinner spinner;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "Main" layout resource
        SetContentView(Resource.Layout.SpinnerControl);

        spinner = FindViewById<Spinner>(Resource.Id.spinner);
        spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);

        adapter = ArrayAdapter.CreateFromResource(
            this, Resource.Array.countries_array, Android.Resource.Layout.SimpleSpinnerItem);

        adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
        adapter.NotifyDataSetChanged();
        spinner.Adapter = adapter;

        spinner.SetSelection(GameSettings.ISettings.CountrySetting);
    }


    private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
    {
        Spinner spinner = (Spinner)sender;
        spinner.SetSelection(adapter.GetPosition(e.Position));
        Console.WriteLine("Setting{0}", GameSettings.ISettings.CountrySetting);
        if (e.Position != GameSettings.ISettings.CountrySetting)
        {
            GameSettings.ISettings.CountrySetting = e.Position;
            Options.countryText.ChangeText("Country: " + System.Environment.NewLine + ScoreHelper.GetCountry(GameSettings.ISettings.CountrySetting));
            ScoreBoards.Board.IsInfoUpdated = true;

        }
    }
}

So how do I update the spinner with the selected item right away when the drop down is closed? 那么当下拉菜单关闭时,如何立即使用所选项目更新微调器?

Apparently this issue only happens on device emulator. 显然这个问题只发生在设备模拟器上。 I did not encounter it when debugging on a physical device. 在物理设备上调试时我没有遇到它。

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

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