AsyncTask 中的进度条未更新

发布于 2024-12-13 01:23:47 字数 912 浏览 4 评论 0原文

我正在尝试在下载一些东西时更新 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 更新它。这可能吗?为什么不更新?

I'm trying to update the progress bar in a AsyncTask while downloading some stuff..
Here is the code:

    @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>

A note: The progress bar is in the layout of the Activity I call in the onPreExecute(). and I want to update it through the AsyncTask. Is this possible? Why isnt it updated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法唧唧 2024-12-20 01:23:47

要更新进度对话框,您需要从 doInBackground 调用 publishProgress(...)
检查 AsyncTask 文档 的“使用情况”和“publishProgress”部分

To update progress dialog you need to call publishProgress(...) from doInBackground.
Check Usage and publishProgress sections of AsyncTask doc

掩耳倾听 2024-12-20 01:23:47

doInBackground 线程无法访问UI 线程。 onProgressUpdate 允许您将更新发布到 UI 线程。您的代码应如下所示:

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

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

}

The doInBackground thread can not access the UI thread. The onProgressUpdate allows you to post updates to the UI thread. This is what your code should look like:

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

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文