进度对话框不会在AsyncTask onProgressUpdate上更新

时间:2012-04-14 09:17:28

标签: android multithreading dialog ui-thread

我正在开发Android 3.1及更高版本的应用程序。

我有这个代码来下载一些json数据:

public class FormsListActivity extends ListActivity
{
    private List<Form> mForms;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.formlist);

      FormListAsyncTask formAsyncTask = new FormListAsyncTask(this);
      formAsyncTask.execute("http://192.168.1.128/RestServiceImpl.svc/forms/");
    }

    private class FormListAsyncTask extends AsyncTask<String, Integer, List<Form>> {

        private Context mContext;
        private ProgressDialog loadingDialog;

        FormListAsyncTask(Context context)
        {
            mContext = context;
            loadingDialog = new ProgressDialog(mContext);
            loadingDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            loadingDialog.setMessage("Downloading. Please wait...");
            loadingDialog.setCancelable(false);
            loadingDialog.setMax(100);
            loadingDialog.show();
        }

        @Override
        protected List<Form> doInBackground(String... url)
        {
            Log.v("doInBackground", "retreiving forms");
            return FormSpringController.LoadAll(url[0]);
        }
        // Just some example code to update your progress dialog
        protected void onProgressUpdate(Integer... values)
        {
            Log.v("onProgressUpdate", "updating: " + ((int) ((values[0] / (float) values[1]) * 100)));
            loadingDialog.setProgress((int) ((values[0] / (float) values[1]) * 100));
        }

        protected void onPostExecute(List<Form> forms)
        {
            if (forms != null)
            {
                ListActivity act = (ListActivity) mContext;
                act.setListAdapter(new FormAdapter(act, R.layout.form_list_item, forms));
                mForms = forms;
            }
            else
            {
                TextView errorMsg = (TextView)
                        ((FormsListActivity) mContext).findViewById(R.id.formErrorMsg);
                errorMsg.setText("Problem downloading forms. Please try again later.");
            }
            loadingDialog.dismiss();
        }
    }
}

为什么在onProgressUpdate上没有更新loadingDialog的进度?

1 个答案:

答案 0 :(得分:3)

我在您的代码中找不到任何publishProgress();

从您的doInBackground()调用publishProgress()隐式调用覆盖onProgressUpdate()