进度对话框未显示在AsyncTask中 [英] Progress Dialog not showing up in AsyncTask

查看:80
本文介绍了进度对话框未显示在AsyncTask中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有AsyncTask的Android应用程序,该应用程序负责从互联网下载文件.单击Listview中的项目时,将执行此AsyncTask.因此,我有一个自定义适配器,并且在Listview的OnItemClickListener中,开始下载并执行AsyncTask.

I have an Android application with an AsyncTask which is responsible for downloading a file from the internet. This AsyncTask is executed when clicking on an item in a Listview. So I have a custom adapter and in the OnItemClickListener of the Listview, I start the download and execute the AsyncTask.

现在,我的适配器包含以下代码以启动名为FileDownloader的AsyncTask:

Now, my adapter contains the following code to start the AsyncTask named FileDownloader:

@Override
public void onClick(View view) {
     try {
         FileDownloader fd = new FileDownloader(activity);
         // some irrelevant code here
         String filepath = fd.execute("http://myurl.com/img.png", PDFFileName, GameHistoryAdapter.this.gameInfo.toString()).get();
     }
     catch(Exception e) { e.printStackTrace(); }
}

活动是一个私有字段,该字段在适配器的构造函数中传递给适配器:

Activity is a private field that is passed to the adapter in the constructor of the adapter:

public GameHistoryAdapter(Activity a, int selectedIndex) {
    this.activity = a;
}

FileDownloader类包含一个OnPreExecute方法,我想在其中显示活动的进度对话框:

The FileDownloader class contains an OnPreExecute method where I want to show the progress dialog on the activity:

@Override
protected void onPreExecute() {
    super.onPreExecute();

    dialog = new ProgressDialog(activity);
    dialog.setMessage("Downloading...");
    dialog.setIndeterminate(true);
    dialog.setCancelable(true);
    dialog.show();
}

但是无论我如何尝试,都不会出现该对话框.当我在AsyncTask的OnPostExecute方法中创建警报对话框时,将显示该对话框.

But whatever I try, the dialog does not appear. When I create an alert dialog in the OnPostExecute method of the AsyncTask, the dialog will show.

@Override
protected void onPostExecute(String res)
{
    super.onPostExecute(res);
    dialog.hide();

    new AlertDialog.Builder(activity)
            .setTitle(activity.getString(R.string.save_pdf_title_text))
            .setMessage(activity.getString(R.string.save_pdf_text) + PDFFileName)
            .setPositiveButton(activity.getString(R.string.close_text), null)
            .setIcon(android.R.drawable.ic_dialog_info)
            .show();
}

有人知道为什么我的活动中未显示该对话框吗?

Does anyone know why the dialog is not appearing on my activity?

推荐答案

有人知道为什么我的活动中未显示该对话框吗?

Does anyone know why the dialog is not appearing on my activity?

是的,下面的代码行...

Yes, the following line of code...

String filepath = fd.execute("http://myurl.com/img.png", PDFFileName, GameHistoryAdapter.this.gameInfo.toString()).get();

不要 EVER 使用AsyncTaskget()方法.它将阻塞主/UI线程,并使AsyncTask的全部内容多余.换句话说,get()将其变成一个同步过程,而不是一个异步过程.

Don't EVER use the get() method of AsyncTask. It will block the main / UI thread and makes the whole point of an AsyncTask redundant. In other words get() turns it into a synchronous process instead of an asynchronous one.

您可以在onPostExecute(...)中显示对话框的事实仅仅是因为将在返回对get()的阻塞调用之后调用该对话框.这意味着主/UI线程将不再被冻结(阻止),并且可以再次进行UI更新.

The fact you can show a dialog in onPostExecute(...) is simply because it will be called after the blocking call to get() has returned. This means the main / UI thread will no longer be frozen (blocked) and UI updates can be made once again.

将呼叫中的get()移至execute(...),而只需使用...

Remove get() from your call to execute(...) and instead just use...

fd.execute("http://myurl.com/img.png", PDFFileName, GameHistoryAdapter.this.gameInfo.toString());

...然后在onPostExecute(...)方法中将filepath变量设置为应该的值.

...then in your onPostExecute(...) method set you filepath variable to what it should be.

我不知道是谁在AsyncTask中添加了get()方法,但是如果我找到了它们,我会说些严肃的话.它几乎没有用处,甚至没有用处,并且使很多人感到困惑.

I don't know who added the get() method to AsyncTask but if I ever find them I'll have some serious words to say. It has little or no use and causes a lot of people a lot of confusion.

这篇关于进度对话框未显示在AsyncTask中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆