gpt4 book ai didi

java - 在异步任务前/后执行方法中使用 ProgressDialog

转载 作者:行者123 更新时间:2023-12-02 00:18:33 24 4
gpt4 key购买 nike

编辑:问题已解决。使其全局化。我现在正在做脸部护理,伙计们。谢谢!

下面是扩展 AsyncTask 的类的一些 fragment 。我想在 preExecute() 方法中启动progressDialog并执行progressDialog.dismiss();在 postExecute() 方法中。我发现的所有例子都说要像下面这样做。我遇到的问题是对话框超出了 onPostExecute 的范围。这是预期的,但所有示例似乎都是这样做的。我还注意到,在导入处有一个小警告标志,表明导入未使用。这个 ProgressDialog 应该起作用吗?我需要传递它吗?

import android.app.ProgressDialog;
...

protected void onPostExecute(Bitmap image){//error when doing this in resetDisplay.... onPostExecute is invoked by the ui thread so this may be why it works here and not in resetDisplay
ImageView imageView=(ImageView) parent.findViewById(R.id.imageDisplay);
imageView.setImageBitmap(image);

dialog.dismiss();

}
protected void onPreExecute(){
ProgressDialog dialog=ProgressDialog.show(parent, "Loading", "Loading the image of the day");
}

最佳答案

看下面的代码

private class ProgressTask extends AsyncTask<String, Void, Boolean> {

private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);

/** progress dialog to show user that the backup is processing. */
/** application context. */

protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}

protected Boolean doInBackground(final String... args) {
try {

/**
* Fetch the RSS Feeds from URL
*/
Utilities.arrayRSS = objRSSFeed
.FetchRSSFeeds(Constants.Feed_URL);
return true;
} catch (Exception e) {
Log.e("tag", "error", e);
return false;
}
}

@Override
protected void onPostExecute(final Boolean success) {

if (dialog.isShowing()) {
dialog.dismiss();
}

if (success) {
// display UI
UpdateDisplay();
}
}
}

关于java - 在异步任务前/后执行方法中使用 ProgressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11468603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com