Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Guys
recently i took the way to get into App Mobile whit Xamarin
Im new with this. so is why i need your support.

Well the case is this
In Visual Studio (whit Xamarin.Android) i would like to populate a Spinner with Key/value , like a combobox in VB.NEt/C#.Net
But is not easy to access to the items
i saw several examples , but all handle just the VALUE or one dimension.

i have this in my code:
C#
 protected override void OnCreate(Bundle bundle)
{
   base.OnCreate(bundle);

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

   // SE CARGAN LOS CONTROLES DEL LAYOUT
        

   Button bRegistrar = FindViewById<Button>(Resource.Id.btnRegistrar);
   Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner1);
   EditText sTarjeta = FindViewById<EditText>(Resource.Id.txtTarjeta);

   //SE LLENA EL SPINNER


  spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(Spinner1_ItemSelected);
 
//spinner.

var adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.listado_productos, Android.Resource.Layout.SimpleSpinnerItem);

            
    
    
   adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);

    spinner.Adapter = adapter;

    //ASIGNAMOS EL EVENTO CLICK 
    bRegistrar.Click += BRegistrar_Click;




  }

private void BRegistrar_Click(object sender, EventArgs e)
  {
            Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner1);
  }


from here... im lost.
i would like to get the key/value and assign to my variable
C#
clsParametros.SKU = spinner.GetThatItemSelectedkeyFromMySpinner();


I appreciate your support
Best regards.

What I have tried:

i were already search in google and here, in codeproject.com but nothing
Posted
Updated 6-Dec-17 4:43am

1 solution

Hi guys i found a solution that Works for me. here is:



var dict = new Dictionary<int, string>();
        dict.Add(1, "Tarjeta1");
        dict.Add(2, "tarjeta2");

spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(Spinner1_ItemSelected);    
ArrayAdapter adapter = new ArrayAdapter(this,Android.Resource.Layout.SimpleSpinnerItem,dict.Values.ToArray());
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;



private void btnButton(Object sender,EventArgs e)
{
int intValue = Convert.ToInt32(spinner.SelectedItem.ToString().Replace("Tarjeta", string.Empty).Trim());
   string strValue = intValue.ToString();
}


i hope this solution could help you.
Best regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900