我正在使用AsyncTask创建列表视图下载程序。我想在下载期间显示列表视图中的进度条百分比,但我的百分比没有增加。我知道在进度对话框中显示百分比,但我不知道如何在列表视图中更新。这是我的代码:
FileDownloadTask.java
onCreateView
DownloadInfoArrayAdapter.Java
public class FileDownloadTask extends AsyncTask<String, Integer, String> {
private static final String TAG = FileDownloadTask.class.getSimpleName();
final DownloadInfo mInfo;
public int progress;
public FileDownloadTask(DownloadInfo info) {
mInfo = info;
}
@Override
protected void onProgressUpdate(Integer... values) {
mInfo.setProgress(values[0]);
mInfo.setFilePercent(values[0]);
ProgressBar bar = mInfo.getProgressBar();
if(bar != null) {
bar.setProgress(mInfo.getProgress());
}
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
String root = Environment.getExternalStorageDirectory().toString();
System.out.println("Downloading");
URL url = new URL(mInfo.getFileUrl());
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
// Output stream to write file
File rootdirectory= new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),"Youtube Videos");
if(!rootdirectory.exists())
{
rootdirectory.mkdirs();
}
String nameoffile= URLUtil.guessFileName(mInfo.getFileUrl(),null, MimeTypeMap.getFileExtensionFromUrl(mInfo.getFileUrl()));
File file= new File(rootdirectory,nameoffile);
file.createNewFile();
mInfo.setDownloadState(DownloadState.DOWNLOADING);
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
progress= (int)((total*1001)/lenghtOfFile);
publishProgress(progress);
mInfo.setFilePercent(progress);
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
mInfo.setDownloadState(DownloadState.COMPLETE);
return null;
}
protected void onPostExecute(String file_url) {
mInfo.setDownloadState(DownloadState.COMPLETE);
// String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.mp4";
}
@Override
protected void onPreExecute() {
mInfo.setDownloadState(DownloadState.DOWNLOADING);
}
}
请帮助!
答案 0 :(得分:0)
而不是构造函数尝试通过doInbackground参数传递数据
FileDownloadTask task = new FileDownloadTask().execute(DownloadInfo info);
@Override
protected String doInBackground(DownloadInfo... params) {
info = params[0];
..
}
答案 1 :(得分:0)
诀窍:不要试图直接在View上设置任何内容。相反,更改数据,在ArrayAdapter上调用notifyDataSetChanged(),并允许它更改视图。例如,
@Override
protected void onProgressUpdate(Integer... values) {
mInfo.setProgress(values[0]);
mInfo.setFilePercent(values[0]);
adapter.notifyDataSetChanged();
}
其中适配器应该是适配器的实例,或者适配器的活动的回调,然后将更新的消息中继到适配器,然后调用重写的getView(int,View,Viewgroup)方法并正确显示更新的数据(我是后者的粉丝)。
(这也是假设你的mInfo实例是你的适配器支持的同一个实例。)
编辑:只是一个想法:您可能还想确保一次只有一个项目下载器的单个实例正在运行,因为如果您继续点击,目前您的下载器会启动多个不同的下载程序,这可能是给你带来一些问题。干杯〜