Elena
Elena

Reputation: 77

Cannot implicitly convert type error on Android Spinner Tutorial

Hi I am trying to do the Spinner Tutorial for the Android. http://docs.xamarin.com/android/tutorials/User_Interface/spinner I am getting the error: Cannot implicitly convert type 'System.EventHandler<Android.Widget.ItemEventArgs>' to 'System.EventHandler<Android.Widget.AdapterView.ItemSelectedEventArgs>' (CS0029) on line 26 of my Activity1.cs. I have just copied the code from the tutorial so I am not sure what I need to change this line to so I can run it. Here is my Activity1.cs :

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace HelloSpinner
{
[Activity (Label = "HelloSpinner", MainLauncher = true)]
public class Activity1 : Activity
{
    int count = 1;

    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<ItemEventArgs> (spinner_ItemSelected); 
        var adapter = ArrayAdapter.CreateFromResource (
            this, Resource.Array.planets_array, Android.Resource.Layout.SimpleSpinnerItem);

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

    private void spinner_ItemSelected (object sender, ItemEventArgs e)
    {
        Spinner spinner = (Spinner)sender;

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

Upvotes: 1

Views: 2312

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502296

The simplest approach is probably to just use the delegate type declared by the event... ideally using the simpler method group conversion syntax:

spinner.ItemSelected += spinner_ItemSelected; 

...

private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)

(Now you just need a using directive for Android.Widget.)

Upvotes: 2

Related Questions