Create a custom handler for a spinner

PrescottChartierPrescottChartier USMember ✭✭
edited September 2015 in Xamarin.Android

I need to create a custom handler so that I can pass by reference all my spinners so I may update them based on selected item on base spinner. My code this far:

                    MyDataset = myServ.GetData( "MyData");
                    List<string> itemsNew = new List<string>(); 
                    foreach (DataRow MyDataRow in MyDataset .Tables[0].Rows)
                    {
                        itemsNew.Add(MyDataRow["TheData"].ToString());

                    };
                    ArrayAdapter adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, itemsNew);
                    MySpinner.Adapter = adapter;

This is what I want to do----->>       MySpinner.ItemSelected += new Handler(mySpinner_ItemSelected(MyFirstSpinner, MySecondSpinner, MyThirdSpinner, MyFourthSpinner));

                }

                catch (Exception e)
                {
                    aLabelResult.Text = e.Message;
                };

    void mySpinner_ItemSelected(ref MyFirstSpinner, ref Spinner MySecondSpinner, ref Spinner MyThirdSpinner, ref Spinner MyFourthSpinner)
    {
    // .... do stuff here 
    }

The error I get is:
The best overloaded method match for 'MyApp.LogonActivity.mySpinner_ItemSelected(ref Android.Widget.Spinner, ref Android.Widget.Spinner, ref Android.Widget.Spinner, ref Android.Widget.Spinner)' has some invalid arguments

Any suggestions?

R/
Scott ...

Posts

  • PrescottChartierPrescottChartier USMember ✭✭
    edited September 2015

    No reason, just a typeo. It's there in my project. Thanks for pointing it out though. Wouldn't want anyone to think I'm totally incompetent. :)

    R/
    Scott ...

  • ChristopherDrososChristopherDrosos GRMember ✭✭
    edited September 2015

    I dont think this is the correct way to set a handler, check this guide https://developer.xamarin.com/guides/android/user_interface/spinner/

  • Hi,

    Thanks for the info, but I've checked out that link in my research. You're right, the way I'm doing it is not the "standard" way to assign a handler to the MySpinner.ItemSelected event. I don't want a "standard" handler, I want a custom handler so I can pass all the spinners I want to manipulate by reference. Getting the value of the selected spinner item is pretty straight forward. I can get it with this code (no handler necessary):

    var selectedvalue = string.Format("{0}", MySpinner.GetItemAtPosition(MySpinner.SelectedItem));

    R/
    Scott ....

  • why you dont assign in every spinner different event handler? and with the delegate command you can be sure that every event handler will be different, check this thread also to understand what i mean https://forums.xamarin.com/discussion/22402/creating-an-list-array-of-buttons-with-onclick-events#latest

  • I've looked at that link too. What I need to do is access each spinner in the event handler of this handler, they are not visible to the default method of the handler ie:

    MySpinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);

        void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            Spinner spinner = (Spinner)sender;
    
            string selectedvalue= string.Format("{0}", spinner.GetItemAtPosition(e.Position));
        //.... do something with MySecondSpinner, etc .....
        }
    

    I can get the itemselected, but now I need to update the other spinners when this one changes its selecteditem, the other spinners are not visible to the handler (out of scope) so I need to build a custom handler where I can pass in the other spinners by reference.

    R/
    Scott ...

  • PrescottChartierPrescottChartier USMember ✭✭
    edited September 2015

    I figured it out, Code is below:

    // MyFirstSpinner is the base spinner
    // Set the ItemSelected event

    MySpinner.ItemSelected += delegate(object sender, AdapterView.ItemSelectedEventArgs args) {
    mySpinner_ItemSelected(ref MyFirstSpinner, ref MySecondSpinner, ref MyThirdSpinner, ref MyFourthSpinner); };

    // The ItemSelected event
    void mySpinner_ItemSelected<One, Two, Three, Four>(ref One MyFirstSpinner, ref Two MySecondSpinner, ref Three MyThirdSpinner, ref Four MyFourthSpinner)
    {

            Spinner myFirstSpinner = MyFirstSpinner as Spinner;
            Spinner mySecondSpinne = MySecondSpinner as Spinner;
            Spinner myThirdSpinner = MyThirdSpinner as Spinner;
            Spinner myFourthSpinner = MyFourthSpinner as Spinner;
    
        // Get the selected value of myFirstSpinner, the base spinner
            var selectedvalue = string.Format("{0}", myLeagueSpinner.GetItemAtPosition(myFirstSpinner.SelectedItemPosition));
    
        // Set up the other spinners with the value of the base spinner
        // ... do that stuff here ....
        }
    

    R/
    Scott ...

Sign In or Register to comment.