Craig D.
Craig D.

Reputation: 3

Binding data from ASP.NET webservice to a spinner in Xamarin

I am currently experimenting with Xamarin Free and am trying to hookup with my webservices (ASP.NET) to populate a spinner.

What I am trying to do is asyncronously call the service and then once the result is returned, populate the Spinner.

What happens is, only one single item is bound - the first element in the result array.

Any advice would be greatly appreciated!

The code is as follows:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using WebServiceTestApplication.ServiceProxy;

namespace WebServiceTestApplication
{
    [Activity (Label = "WebServiceTestApplication", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

        Tablet_Services proxy; 
        Spinner spCategories;
        ArrayAdapter<string> spCategoriesContent;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.Main);
            proxy  = new ServiceProxy.Tablet_Services();

            spCategoriesContent = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleSpinnerItem);
            spCategoriesContent.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);

            spCategories = (Spinner)FindViewById (Resource.Id.spCategories);
            spCategories.Adapter = spCategoriesContent;

            UpdateTreatmentCategories ();
        }

        void PopulateResults (srTreatmentCategory[] result)
        { 
            if (result != null) 
            {
                foreach (srTreatmentCategory c in result) 
                {
                    Console.WriteLine ("Adding {0} to spCategoriesContent", c.CategoryName);
                    spCategoriesContent.Add(c.CategoryName);
                }
            }
        }

        public void UpdateTreatmentCategories ()
        {
            proxy.BegingetTreatmentCategories (delegate (IAsyncResult ar) {
                var result = proxy.EndgetTreatmentCategories (ar);
                PopulateResults (result);
            }, null);
        }
    }
}

Upvotes: 0

Views: 1021

Answers (2)

Val Okafor
Val Okafor

Reputation: 3447

Here is what works for me for a drop down list called Category. I think you are in the right track by having a method you called PopulateResults() I called mine LoadSpinnerData().

CategorySpinner = dialogView.FindViewById<Spinner>(Resource.Id.spinnerCategory);
CategorySpinner.ItemSelected += spinner_ItemSelected;

private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            var spinner = (Spinner)sender;
            SelectedCategory = string.Format("{0}", spinner.GetItemAtPosition(e.Position));
}
private void LoadSpinnerData()
        {
            var tempCategories = (List<ServiceCategory>) CategoryManager.GetCategories();
            var categories = tempCategories.Select(category => category.Name).ToList();

            var categoryAdapter = new ArrayAdapter<string>(
                Activity, Android.Resource.Layout.SimpleSpinnerItem, categories);

            categoryAdapter.SetDropDownViewResource
                (Android.Resource.Layout.SimpleSpinnerDropDownItem);
            CategorySpinner.Adapter = categoryAdapter;
        }

You can find the whole code file here https://github.com/valokafor/XamarinAndroidCustomDialog/blob/master/XamarinDroidCustomListView/ServiceDialog.cs

Good luck with Xamarin

Upvotes: 1

aldorain
aldorain

Reputation: 790

You are updating the model list but not updating the adapter. Try updating the adapter with loaded data.

Edit Here's the simplest example.

    void PopulateResults (srTreatmentCategory[] result)
    { 
        if (result != null) 
        {
            foreach (srTreatmentCategory c in result) 
            {
                Console.WriteLine ("Adding {0} to spCategoriesContent", c.CategoryName);
                spCategoriesContent.Add(c.CategoryName);
            }

            spCategoriesContent = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleSpinnerItem);
            spCategoriesContent.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
            spCategories.Adapter = spCategoriesContent;
        }
    }

In the end you should consider writing more your own Adapter implementation with updateData / swapData method.

Upvotes: 0

Related Questions