0

I have created a Spinner successfully which contains the Items: Hello1, Hello2 and Hello3.

Now I try to create an "OnItemSelectedListener" event so when one click on an item, I try to catch this item in the "spinnerItemClick" event.
(I have a custom adapter but the problem would be focused on the: OnItemSelectedListener)

But for the below line, I get this error. So I am not sure how to set this up as I try below?

spinner.OnItemSelectedListener = spinnerItemClick;


Complete code:

 void createSpinner()
        {
            Android.Content.Context context = ApplicationContext;
            Spinner spinner = new Spinner(context);
            spinner.Clickable = true;
            spinner.OnItemSelectedListener = spinnerItemClick;


            Adapter1 adapter1; List<String> list1 = new List<String>(); list1.Add("Hello1"); list1.Add("Hello2"); list1.Add("Hello3");
            adapter1 = new Adapter1(this, Android.Resource.Layout.SimpleListItem1, list1, this, "1");
            spinner.Adapter = adapter1;
        }
        void spinnerItemClick(object sender, AdapterView.IOnItemClickListener e)
        {
            //Here how to get the item that is clicked?
        }


I have tried to do the below code also but as seen my compiler underlines alot of things in red. So I am not sure why this is happening:
(The easisest is to show an image here I think)
enter image description here


Spinner does not contains a defintion for 'setOnItemSelectedListener' and no extension method 'setOnItemSelectedListener' accepting a first argument of type 'Spinner' could be found (are you missing a using directive or an assembly reference.

Name name '@Override' does not exist in the current context

Andreas
  • 1,121
  • 4
  • 17
  • 34

1 Answers1

0

When I googled on Xamarin for the selected item. I actually find an answer that gives the position index in the spinner like this:

            spinner.ItemSelected += (sender, args) =>
            {
                //args.Position gives the index in the list
            };
Andreas
  • 1,121
  • 4
  • 17
  • 34