Java 异步任务和自定义侦听器
我有一个带有Java 异步任务和自定义侦听器,java,android,android-asynctask,orientation,Java,Android,Android Asynctask,Orientation,我有一个带有单选按钮的列表。当用户选择一个项目时,AsyncTask应该启动。我创建了一个接口来向适配器发送事件。下载开始时,应打开ProgressDialog。屏幕旋转后,我无法获取参考进度对话框。日志中显示progressDialog!=当对话框从窗口中消失时,null和progressDialog.isShowing=true。我如何检索此引用,以及为什么在我打开屏幕时对话框仍保持其状态 适配器: ... convertView.setOnClickListener(new View.On
单选按钮的列表
。当用户选择一个项目时,AsyncTask
应该启动。我创建了一个接口来向适配器发送事件。下载开始时,应打开ProgressDialog
。屏幕旋转后,我无法获取参考进度对话框。日志中显示progressDialog!=当对话框从窗口中消失时,null
和progressDialog.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]);
}
我认为你应该在做任何事情之前把对话分配给ProgressDialog
private ProgressDialog dialog;
把这个换成这个:
final ProgressDialog dialog = new ProgressDialog(getContext());
final ProgressDialog dialog = new ProgressDialog(getActivity());
对于这一点:
final ProgressDialog dialog = new ProgressDialog(getContext());
final ProgressDialog dialog = new ProgressDialog(getActivity());
我认为你应该在做任何事情之前把对话分配给ProgressDialog
private ProgressDialog dialog;
把这个换成这个:
final ProgressDialog dialog = new ProgressDialog(getContext());
final ProgressDialog dialog = new ProgressDialog(getActivity());
对于这一点:
final ProgressDialog dialog = new ProgressDialog(getContext());
final ProgressDialog dialog = new ProgressDialog(getActivity());