我需要使用来自服务器的数据列表来构建 BaseExpandableListAdapters。我正在使用 AsyncTask 来检索这个值列表。由于我目前无法从 DoInBackGround 覆盖方法返回 BaseExpandableListAdapter,因此我正在使用公共属性。使用以下代码,我可以:
- 获取我的 AsyncTask 类的 My 和 Market 属性以返回所有预期值但我的 ProgressDialog 直到任务执行后才会出现,使其无用,或者
- 让 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();
}
}