1

I need to get the selected item off my spinner but I am getting the above error and I don't know why.

So, I am expecting to get the spinner selected item without calling the spinner's event handler, if that's possible, because I need to use it on my button click.

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

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

        Button bt_ok = FindViewById<Button>(Resource.Id.bt_ok);
        Spinner spinner = (Spinner)FindViewById<Spinner>(Resource.Id.dd_spinner);

         //
         //getSelectedItem does not contain a definition!!!
         //
        String spinner_text = spinner.getSelectedItem().toString();

         //
         //Do I need this? 
         //
        spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);


        var adapter = ArrayAdapter.CreateFromResource(
                this, Resource.Array.planet_array, global::Android.Resource.Layout.SimpleSpinnerItem);
        adapter.SetDropDownViewResource(global::Android.Resource.Layout.SimpleSpinnerDropDownItem);
        spinner.Adapter = adapter;

        bt_ok.Click += delegate
        {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.SetTitle("Confirm");

            //
            //I need the value here!!!
            //
            builder.SetMessage(new Confirm().Choice(spinner_text));


            builder.SetCancelable(true);
            builder.SetPositiveButton("Yes", delegate { });
            builder.Show();
        };
    }

I tried this and this answers, but I couldn't get it working.

Lucas
  • 599
  • 7
  • 20
  • You should consider familiarizing yourself with [the Xamarin documentation](https://developer.xamarin.com//). Scanning the API documentation can help you figure out these differences between the Android Java API and the Xamarin C# API. – Code-Apprentice Jul 28 '17 at 06:15

1 Answers1

3

The Java getSelectedItem method in Xamarin.Android is transformed to a C# read-only property (SelectedItem):

var spinner = FindViewById<Spinner>(Resource.Id.dd_spinner);
var selectedItem = spinner.SelectedItem;
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • It's returning null. The spinner is loading everthing alright, but when I click the button it's breaking the app now. – Lucas Jul 28 '17 at 03:03
  • Are you using your identifier? `Resource.Id.dd_spinner` I typed a generic one, but edited to use the one amor your code.... – SushiHangover Jul 28 '17 at 03:07
  • Yes.. Here's my code... Spinner spinner = (Spinner)FindViewById(Resource.Id.dd_spinner); var spinner_text = spinner.SelectedItem; – Lucas Jul 28 '17 at 03:09
  • It worked when I moved the second line to the button click block. Thank you very much! – Lucas Jul 28 '17 at 03:19
  • 1
    @Lucas Ahh.. `SelectedItem` and thus `spinner_text` will return null until you set a value on it (either a default value or via user selection) – SushiHangover Jul 28 '17 at 03:21