我有一个在后台运行的下载过程,并使用进度更新 UI(ListView 适配器)。在我离开活动并回来之前它工作正常。再次加载活动后,会出现一个“新”ListView 对象,该对象与绑定到 BG 下载过程的对象不同。如何构建我的代码,以便后台进程始终可以与我的活动中的 ListView 对话?
执行此操作的特定行是:
adapter.notifyDataSetChanged();
这是下载类的外壳:
public class Download
{
}
protected void start()
{
TransferManager tx = new TransferManager(credentials);
this.download = tx.download(s3_bucket, s3_dir + arr_videos.get(position), new_video_file);
download.addProgressListener(new ProgressListener()
{
public void progressChanged(final ProgressEvent pe)
{
handler.post( new Runnable()
{
@Override
public void run()
{
if ( pe.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE )
{
Download.this.onComplete();
}
else
{
Download.this.onProgressUpdate();
}
}
});
}
});
}
protected void onProgressUpdate()
{
this.download_status = "downloading";
Double progress = this.download.getProgress().getPercentTransfered();
Integer percent = progress.intValue();
//Log.v("runnable", percent + "");
downloaded_data.edit().putInt(position+"", percent).commit();
adapter.notifyDataSetChanged();
}
}