MonoDroid异步Web服务调用

时间:2011-04-24 05:13:46

标签: progress-bar xamarin.android

我试图在异步webservice调用之后显示列表视图,但由于某种原因,此代码失败....任何建议?现在,它只是崩溃了应用程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

namespace Test
{
    [Activity(Label = "Favorites")]
    public class FavoritesActivity : ListActivity
    {
        private ProgressDialog d;
        private TextView tv;
        protected override void OnCreate(Bundle bundle)
        {

            base.OnCreate(bundle);

            d = new ProgressDialog(this);
            d.SetTitle("Loading...");
            d.SetMessage("Please wait...");
            d.SetIcon(Resource.Drawable.chef_icon);
            d.Show();



            Test.MyWeb.Daedalus Service = new Daedalus();

            Service.getCategoriesCompleted += Service_OnComplete;
            Service.getCategories();


        }

        private void Service_OnComplete(object s, getCategoriesCompletedEventArgs e)
        {

            try
            {
                d.Hide();

                List<Test.MyWeb.CATEGORY> Categories = e.Result.ToList();
                List<string> cats = new List<string>();

                foreach (Test.MyWeb.CATEGORY Item in Categories)
                {
                    cats.Add(Item.NAME);

                }

                ListAdapter = new ArrayAdapter<string>(this, Resource.Layout.Favorites, cats);

                ListView.TextFilterEnabled = true;

                ListView.ItemClick += delegate(object sender, ItemEventArgs args)
                {
                    // When clicked, show a toast with the TextView text
                    Toast.MakeText(Application, ((TextView)args.View).Text, ToastLength.Short).
                        Show();
                };



            } catch(Exception ex)
            {
                Toast.MakeText(this, ex.Message, ToastLength.Short).Show();

            }
        }

    }
}

1 个答案:

答案 0 :(得分:4)

如果它确实是异步调用,则需要修改UI线程上的UI,而不是后台线程:

http://mono-android.net/Documentation/Guides/Writing_Responsive_Applications

无论如何,您应该能够在Android日志中找到错误:

http://mono-android.net/Documentation/Guides/Android_Debug_Log

相关问题