【问题标题】:AsyncTask and custom listenerAsyncTask 和自定义监听器
【发布时间】:2016-02-25 08:28:01
【问题描述】:

我有一个带有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();
    }
});
...

异步任务:

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();
}

【问题讨论】:

    标签: java android android-asynctask orientation


    【解决方案1】:

    我认为您应该先将对话框分配给 ProgressDialog

    private ProgressDialog dialog;
    

    然后从这个变成这个:

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

    到这个:

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

    【讨论】:

      猜你喜欢
      • 2014-08-07
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 2017-02-18
      • 2011-12-24
      相关资源
      最近更新 更多