2

Just looking to see if I can find out how i get the item selected in the spinner and store it in a string, i have seen the other posts about this and people say to put this line into code: (Beneath the last line of the code i posted below)

String Genders = Gender.getSelectedItem().toString();

I try but it gives me a red line underneath (getselecteditem()) - saying spinner does not contain a definition for getselecteditem()

Here is my code:

  Spinner Gender;
  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Form);

        var Genders = new String[]
        {
            "Male", "Female"
        };
        BaseMale = 2000;
        Gender = FindViewById<Spinner>(Resource.Id.spinner1);
        Gender.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, Genders);

Would really appreciate any of your help! :)

1 Answers1

4

you might need to implement the selection handle (ItemSelected) like in this example from https://developer.xamarin.com/guides/android/user_interface/spinner/:

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

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

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

        spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs> (spinner_ItemSelected);
        var adapter = ArrayAdapter.CreateFromResource (
                this, Resource.Array.planets_array, Android.Resource.Layout.SimpleSpinnerItem);

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

and here is the handle, the index to selected item appears as e.Position here.

private void spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
{
        Spinner spinner = (Spinner)sender;

        string toast = string.Format ("The planet is {0}", spinner.GetItemAtPosition (e.Position));
        Toast.MakeText (this, toast, ToastLength.Long).Show ();
}
Mobigital
  • 749
  • 7
  • 14