将数据从ASP.NET Web服务绑定到Xamarin中的微调器

时间:2015-02-12 14:48:36

标签: android asp.net web-services xamarin android-spinner

我目前正在尝试使用Xamarin Free,并尝试与我的webservices(ASP.NET)连接以填充微调器。

我想要做的是异步调用服务,然后一旦返回结果,填充Spinner。

结果是,只绑定了一个项目 - 结果数组中的第一个元素。

非常感谢任何建议!

代码如下:

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);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

以下是名为Category的下拉列表对我有用的内容。我认为你通过一个叫做PopulateResults()的方法就是在正确的轨道上我称之为我的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;
        }

您可以在https://github.com/valokafor/XamarinAndroidCustomDialog/blob/master/XamarinDroidCustomListView/ServiceDialog.cs

找到整个代码文件

祝Xamarin好运

答案 1 :(得分:0)

您正在更新模型列表但不更新适配器。尝试使用加载的数据更新适配器。

修改 这是最简单的例子。

    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;
        }
    }

最后,您应该考虑使用Adapter / updateData方法编写更多自己的swapData实施方案。