Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner OnItemSelectedListener

I can't find an example of how to do this in mono.

Any help?

Edit: Added code

foreach (equip item in list)
   {
        tr = new TableRow(this);

        sp = new Spinner(this);
        sp.LayoutParameters = lp2;
        sp.Adapter = adapter;                                  
        sp.ItemSelected += new EventHandler<ItemEventArgs>(spinner_ItemSelected());
        sp.SetSelection(Convert.ToInt32(item.status));
        tr.AddView(sp);
   }

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

        string toast = string.Format ("You selected {0}", spinner.GetItemAtPosition (e.Position));
        Toast.MakeText (this, toast, ToastLength.Long).Show ();
    }
like image 522
jmease Avatar asked Jan 11 '12 21:01

jmease


2 Answers

In Mono for Android a lot of listener interfaces have been translated to C# events, including this one. In this case you can hook into the ItemSelected event instead of having to create a listener. Xamarin has a full example of using a spinner available here.

Edit:

Based on your request to differentiate when the spinner value has actually changed, you can do something like:

int initialSpinnerPosition = spinner.SelectedItemPosition;
spinner.ItemSelected += (sender, args) =>
                        {
                            if (args.Position != initialSpinnerPosition)
                            {
                                // do stuff
                            }
                        };
like image 102
Greg Shackles Avatar answered Sep 20 '22 12:09

Greg Shackles


you can use the tag like

bool userClick = false;

spinner.ItemSelected += (sender, e) => {
            if(userClick) {
                --- do stuff
            }
            userClick = true;
        };

When you create/update the spinners adapter based on a SQL Data

userClick = false;
like image 36
AAli Avatar answered Sep 20 '22 12:09

AAli