2012-09-10 36 views
0

我需要使用服務器中的數據列表來構建BaseExpandableListAdapters。我使用AsyncTask來檢索這個值列表。由於我目前無法從DoInBackGround覆蓋方法返回BaseExpandableListAdapter,因此我正在使用公共屬性。下面的代碼,我可以:AsyncTasks和ProgressDialogs

  1. 把我的AsyncTask類的我和市場特性與所有預期值迴歸,但我的ProgressDialog不會出現在任務被執行後,使之無用,直到OR
  2. 讓ProgressDialog立即按需要顯示,但當我嘗試使用它們將適配器分配給我的列表視圖時,我的和市場屬性爲空

我需要實現兩者。

public class Work : Activity 
{ 
    private ExpandableListView market; 
    private ExpandableListView my; 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     SetContentView(Resource.Layout.Work); 

     market = (ExpandableListView)FindViewById(Resource.Id.market); 
     my = (ExpandableListView)FindViewById(Resource.Id.my); 

     PopulateWorkListViews populate = new PopulateWorkListViews(this); 
     populate.Execute(techID, market, userType); 

     //Removing the following line allows for the ProgressDialog but the Market 
     //and My properties of the PopulateWorkListViews class are empty as opposed 
     //to leaving this line in and both properties contain all expected values 
     //but ProgressDialog doesn't show up. 
     string result = populate.Get().ToString(); 

     market.SetAdapter(populate.Market); 
     my.SetAdapter(populate.My); 
    } 
} 

public class PopulateWorkListViews : AsyncTask 
{ 
    private Context Context { get; set; } 
    private ProgressDialog Dialog { get; set; } 
    public BaseExpandableListAdapter Market { get; set; } 
    public BaseExpandableListAdapter My { get; set; } 

    public PopulateWorkListViews(Context context) 
    { 
     Context = context; 
    } 

    protected override void OnPreExecute() 
    { 
     Dialog = new ProgressDialog(Context); 
     Dialog.SetMessage("Processing..."); 
     Dialog.Window.SetGravity(GravityFlags.Center); 
     Dialog.SetCancelable(false); 
     Dialog.Indeterminate = true; 
     Dialog.SetProgressStyle(ProgressDialogStyle.Spinner); 
     Dialog.Show(); 
    } 

    protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params) 
    { 
     WS ws = new WS(); 
     List<ServiceCall> calls = ws.GetMarketCalls(@params[0].ToString(), @params[1].ToString(), @params[2].ToString()).ToList(); 

     Market = new MarketCallListView(Context, calls); 
     My = new MyCallListView(Context, calls, @params[0].ToString()); 

     return "Done"; 
    } 

    protected override void OnPostExecute(Java.Lang.Object result) 
    { 
     Dialog.Dismiss(); 
    } 
} 

回答

0

對話框沒有被立即繪製的原因是您在屏蔽populate.Get()的UI線程。如果將onCreate底部的兩條SetAdapter行移動到onPostExecute,會發生什麼情況?

+0

似乎工作。謝謝! – jmease