AsyncTask和自定义侦听器

时间:2016-02-25 08:28:02

标签: java android android-asynctask orientation

我有一个RadioButtons的列表。当用户选择项目时,AsyncTask应该开始。我创建了一个接口来向适配器发送事件。下载开始时,应该打开ProgressDialog。屏幕旋转后我无法获得参考ProgressDialog。在日志中,当对话框从窗口中消失时,它会显示progressDialog != nullprogressDialog.isShowing = true。当我翻开屏幕时,如何检索此参考以及对​​话为何保持其状态?

适配器:

...
convertView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final ProgressDialog dialog = new ProgressDialog(getContext());
        dialog.setIndeterminate(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        Log.d(LOG_TAG, getClass().getSimpleName() + ": click ");
        checked = position;
        notifyDataSetChanged();
        if (translationDownloadTask != null) {
            translationDownloadTask.cancel(true);
        }
        translationDownloadTask = new TranslationDownloadTask(translations[position], translations[position]);
        translationDownloadTask.setListener(new OnFileDownloadListener() {
            @Override
            public void onStartDownloading(String title) {
                dialog.show();
            }
            @Override
            public void onProgress(String title, int progress) {
                dialog.setProgress(progress);
                Log.d(LOG_TAG, getClass().getSimpleName() + ": progressDialog " + dialog.isShowing());
            }
            @Override
            public void onDonwloadingComplete() {
            }
            @Override
            public void onError() {
            }
        });
        translationDownloadTask.execute();
    }
});
...

的AsyncTask:

public class TranslationDownloadTask extends AsyncTask<Void, Integer, Void> {
private static final String LOG_TAG = "myQuranTag";
String url;
String progressDialogTitle;
OnFileDownloadListener listener;

public void setListener(OnFileDownloadListener listener){
    this.listener = listener;
}

public TranslationDownloadTask(String url, String title) {
    this.url = url;
    this.progressDialogTitle = title;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    listener.onStartDownloading(progressDialogTitle);
    Log.d(LOG_TAG, getClass().getSimpleName() + ": Начало " );
}

@Override
protected Void doInBackground(Void... params) {
    int lenght = 30;
    for (int i = 0; i < lenght; i++) {
        try {
            if (!isCancelled()){
                Thread.sleep(200);
                int progress = i*100/lenght;
                publishProgress(progress);
            }else {
                listener.onError();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    listener.onDonwloadingComplete();
    Log.d(LOG_TAG, getClass().getSimpleName() + ": Задача завершена ");
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    listener.onProgress(progressDialogTitle, values[0]);
}

}

OnFileDownloadListener:

public interface OnFileDownloadListener {
    void onStartDownloading(String title);
    void onProgress(String title, int progress);
    void onDonwloadingComplete();
    void onError();
}

1 个答案:

答案 0 :(得分:0)

我认为您应该在任何事情之前将对话框分配给ProgressDialog

private ProgressDialog dialog;

并从这一个改为这个:

final ProgressDialog dialog = new ProgressDialog(getContext());

到这一个:

final ProgressDialog dialog = new ProgressDialog(getActivity());