Add items to spinner

MarekCernMarekCern USMember

Okay here I am screwed agin. I wanna ask how to add items to a spinner in code (in documentation was something about using spinner.SetAdapter() but I dont have that method here :O Thanks for every advise.

Posts

  • ChrisHonselaarChrisHonselaar ✭✭✭ NLBeta ✭✭✭

    Instead of the Spinner.setAdapter() method, in C# you simply use the Spinner.Adapter property.

    In general, if you are missing a method/field/etc, use IntelliSense and look for the 'C# beautified' version of the Java method/field.

    Read this document to get more understanding of how the Xamarin APIs are designed and based on their Java equivalents.

  • MarekCernMarekCern USMember

    So theres no normal method to add one item to a spinner like in WinForm? :O Or is it and I just have to find it?

  • FZelleFZelle ✭✭✭ DEMember ✭✭✭

    No, you really have to use an Adapter.
    But to create a simple List Adapter is easy and then you can have an Add function that also calls NotifyDataSetChanged.

  • ChrisHonselaarChrisHonselaar ✭✭✭ NLBeta ✭✭✭

    You can just pass it an ArrayAdapter based on a simple list of items. How much easier do you want it? :)

  • MarekCernMarekCern USMember

    I know I must seem really dense but I was used to WinForm and this is my beginning with something else.. :) Can you please correct me with this? :D

          ArrayAdapter lol = new ArrayAdapter(this,"What is this number? xD");
          lol.Add("lol");
          spinner.Adapter = lol;
    
  • MarekCernMarekCern USMember

    string[] data = {"one", "two", "three", "four", "five"} ;
    ArrayAdapter adapter = new ArrayAdapter (this,
    Resource.Layout.ZapisZnamky, data);
    tridy.Adapter = adapter;

    So whats wrong now? :(

  • ChrisHonselaarChrisHonselaar ✭✭✭ NLBeta ✭✭✭

    You're not giving us much information here...

  • MarekCernMarekCern USMember

    Theres nothing more to post :O I have a spinner named "tridy" and I want to get the items (one, two..) to him and I am doing it in OnCreate of the "ZapisZnamky" layout. This is all code that I need, isnt it? :O

    string[] data = {"one", "two", "three", "four", "five"} ;
    ArrayAdapter adapter = new ArrayAdapter (this, Resource.Layout.ZapisZnamky, data);
    tridy.Adapter = adapter;

  • FZelleFZelle ✭✭✭ DEMember ✭✭✭

    If your spinner is called tridy and Resource.Layout.ZapisZnamky is a lineLayout for the spinner.

    But everything is explained in the two links that chris gave you already.

  • MarekCernMarekCern USMember

    Yeah, sorry - I finally got it ;) thank you for your patience.. :D

  • coder_rana007coder_rana007 ✭✭ USMember ✭✭

    I am trying to use spinner but item not add and also not run when i try to debug please some one help me

  • coder_rana007coder_rana007 ✭✭ USMember ✭✭

    my code is here but not working

            Spinner spn = FindViewById<Spinner>(Resource.Id.spinner1);
    
            string[] data = { "one", "two", "three", "four", "five" };
            ArrayAdapter adapter = new ArrayAdapter(this, Resource.Id.spinner1, data);
            spn.Adapter = adapter;
    
  • shah_hassan123shah_hassan123 PKMember

    // This code is working and it add list to spinner dynamically.
    Spinner spinner;
    spinner = FindViewById (Resource.Id.spinner1);
    var items = new List() {"one", "two", "three"};
    var adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, items);
    spinner.Adapter = adapter;

  • ParthaSarathyParthaSarathy ✭✭ USMember ✭✭

    ListSpinnerList = new List();

    void LoadSpinner()
    {
    //Create List For Bing your Own Data from Datta base

    try
    {
    for (int icount = 0; icount < List.Count; icount++)
    {
    //Bind the Data into Spinner list from the Datatbase List
    SpinnerList.Add(List[icount].BookName);
    }

    //Code For Binding List into Spinner
    ArrayAdapter adapter = new ArrayAdapter(this, Android.Resource.Layout.SelectDialogItem, SpinnerList);
    adapter.SetDropDownViewResource(Android.Resource.Layout.SelectDialogItem);
    FindViewById(Resource.Id.CustomCodebook_linear).Visibility = ViewStates.Visible;
    Spinner spinner = FindViewById(Resource.Id.spn_Category);
    spinner.Adapter = adapter;

    //Code for Evet for Changing Spinner Item
    spinner.ItemSelected -= violationcode_ItemSelected;
    spinner.ItemSelected += violationcode_ItemSelected;
    }
    catch (Exception exe)
    {
    ViewModel.PageNavagationAlert(exe.Message);
    }
    }

    // Code When Item Changed in Spinner, want to get the selected Value
    private void violationcode_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
    {
    try
    {
    Spinner resultspinner = (Spinner)sender;
    int position = resultspinner.SelectedItemPosition;
    String Result =SpinnerList [position].BookName;
    }
    catch (Exception exe)
    {
    ViewModel.PageNavagationAlert(exe.Message);
    }
    }

  • ilyasilyas ✭✭ USMember ✭✭
    edited April 2016

    // It Working Sure;
    Spinner spinner = FindViewById(Resource.Id.spinnerId);
    List mitems = new List();
    mitems.Add("value1");
    mitems.Add("value2");
    mitems.Add("value3");
    mitems.Add("value4");
    ArrayAdapter adapter = new ArrayAdapter (Activity, Android.Resource.Layout.SimpleSpinnerItem, mitems);
    spinner.Adapter = adapter;

  • JoeyyyyJoeyyyy ✭✭ MYMember ✭✭

    ``Add this in Main.axml
    <?xml version="1.0" encoding="utf-8"?>



    Add this in Strings.xml
    <?xml version="1.0" encoding="utf-8"?>

    Home

    Home
    Home Finishing
    Professional Services
    Advertise With Us
    Group Sites



    Finally Add this in Main.cs

    using System;

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

    namespace menuBar.Droid
    {
    [Activity (Label = "", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
    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<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);
            var adapter = ArrayAdapter.CreateFromResource(
                    this, Resource.Array.home_array, Android.Resource.Layout.SimpleSpinnerItem);
    
            adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            spinner.Adapter = adapter;
        }
    
        private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            Spinner spinner = (Spinner)sender;
    
            string toast = string.Format("The service is {0}", spinner.GetItemAtPosition(e.Position));
            Toast.MakeText(this, toast, ToastLength.Long).Show();
        }
    }
    

    }
    ``

    Change According to your needs.Hope this may help u

  • shubham1shubham1 ✭✭✭ Member ✭✭✭

    @Joeyyyy
    can you tell me how could I implement a spinner in xamarin.forms?
    I have posted a question on forums

    https://forums.xamarin.com/discussion/comment/342517#Comment_342517
    please suggest me something.

  • SzbestaSzbesta ✭✭✭ Member ✭✭✭

    Dude this is xamarin.android dont expect an answer for xamarin forms here....

Sign In or Register to comment.