【问题标题】:Progress Bar not updating in AsyncTask进度条未在 AsyncTask 中更新
【发布时间】:2011-12-17 15:22:41
【问题描述】:

我正在尝试在下载一些内容时更新 AsyncTask 中的进度条..
这是代码:

    @Override
protected void onPreExecute() {
    Intent myIntent = new Intent(mContext, Explanation.class);
    mContext.startActivity(myIntent);

    View inflater = View.inflate(mContext, R.layout.explanation, null);
    mProgressDialog = (ProgressBar) inflater.findViewById(R.id.Explanation_ProgressBar);
    }

@Override
protected void onProgressUpdate(Integer... values) {
    mProgressDialog.setProgress(values[0]);

}

xml:

  <ProgressBar 
   android:max="100"       
style="?android:attr/progressBarStyleHorizontal"
    android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:id="@+id/Explanation_ProgressBar" 
     android:layout_height="wrap_content"></ProgressBar>

注意:进度条位于我在 onPreExecute() 中调用的 Activity 的布局中。我想通过 AsyncTask 更新它。这可能吗?为什么不更新?

【问题讨论】:

  • 我认为它可能必须在主线程上修改 ui 元素。
  • AsyncTask 是否应该处理所有线程的东西?
  • 别管我以前的cmets我读错了你的问题

标签: android progress


【解决方案1】:

doInBackground 线程无法访问 UI 线程。 onProgressUpdate 允许您将更新发布到 UI 线程。你的代码应该是这样的:

@Override
protected Boolean doInBackground(String... strings) {       
    this.publishProgress(progress);
}   

@Override
protected void onProgressUpdate(Integer... values) {
    mProgressDialog.setProgress(values[0]);

}

【讨论】:

    【解决方案2】:

    要更新进度对话框,您需要从doInBackground 调用publishProgress(...)。 检查AsyncTask doc的Usage和publishProgress部分

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多