我遇到AsyncTask
的问题。
我通过调用webService来更新对象列表。在此期间,我想显示ProgressDialog
。然后当列表完全更新时,我希望关闭对话框和AsyncTask。
问题是UIThread不等待AsyncTask
完成。我看到get()
方法可以为我工作,但是有了它,UIThread就被阻止了,对话框就没有了。
我想知道如何在显示ProgressDialog时等待任务完成。
这是我的AsyncTask类:
public class MyAsyncTask extends AsyncTask<Integer, Void, Boolean>{
private ProgressDialog pd;
private String m_sShelfCode;
private String m_sFamiliyCode;
public MyAsyncTask(String shelfCode, String familyCode) {
this.m_sShelfCode = shelfCode;
this.m_sFamiliyCode = familyCode;
}
@Override
protected void onPreExecute() {
AppManager app = AppManager.getInstance();
LayoutInflater inflater = (LayoutInflater)app.m_AppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout rl = (RelativeLayout)inflater.inflate(R.layout.progress_dialog, null, false);
pd = new ProgressDialog(app.m_AppContext);
pd.requestWindowFeature(Window.FEATURE_NO_TITLE);
pd.setCancelable(false);
pd.show();
pd.setContentView(rl);
}
@Override
protected Boolean doInBackground(Integer... params) {
Log.d("DO IN BACKGROUND","param[0]="+params[0]);
switch (params[0]) {
case AppManager.SHELF_LIST:
//Calling WebService
SoapShelf s = new SoapShelf();
try {
ArticleListFragment.m_ShelfList = s.getShelfList();
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (XmlPullParserException e) {
e.printStackTrace();
return false;
}
break;
case AppManager.FAMILY_LIST:
// set family list
break;
case AppManager.ARTICLE_LIST:
// set article list
break;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
pd.dismiss();
if (result == false)
;// error dialog
}
}
我在这里的ArticleListFragment中调用了这个
public void getItemList(int listType) {
switch (listType) {
case AppManager.SHELF_LIST:
new MyAsyncTask(null, null).execute(new Integer[]{AppManager.SHELF_LIST});
提前致谢
答案 0 :(得分:1)
尝试:
@Override
protected void onPreExecute() {
pd = ProgressDialog.show(context, "title", "message");
}
@Override
protected void onPostExecute(Boolean result) {
if (pd != null && pd.isShowing())
pd.dismiss();
}