【问题标题】:Wait AsyncTask with dialog等待带有对话框的 AsyncTask
【发布时间】:2013-09-03 13:40:06
【问题描述】:

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

提前致谢

【问题讨论】:

  • 也许在显示之前设置对话框的内容视图会有所帮助
  • 不,它会引发异常。它需要是这样的。该对话框有效,我在我的应用程序的其他地方使用它。
  • 抛出什么样的异常
  • android.util.AndroidRuntimeException: requestFeature() must be called before adding content
  • 你能发布你的完整代码和 logcat 吗,我猜你没有在 setContentView() 之前调用 requestFeature()。

标签: java android android-asynctask progressdialog


【解决方案1】:

尝试:

@Override
protected void onPreExecute() {
    pd = ProgressDialog.show(context, "title", "message");
}

@Override
protected void onPostExecute(Boolean result) {
  if (pd != null && pd.isShowing())
    pd.dismiss();
}

【讨论】:

  • 和以前一样:s。当我在调用 AsyncTask 后将适配器设置为此列表时,列表为空。当我刷新列表时,列表已填满。你认为问题可能来自对话?
  • 如果适配器的数据发生了变化,你应该在 onPostExecute 中调用 adapter.notifyDataSetChanged() 。或设置列表适配器 onPostExecute
  • 哇,原来如此。我太傻了。无论如何,谢谢大佬。我已经摆脱了自己的混乱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 2021-09-11
  • 2016-09-21
相关资源
最近更新 更多