【发布时间】:2014-01-12 17:07:42
【问题描述】:
我们如何更新 ListView 中的进度条。当每个进度条与文件的下载相关联并且通过 AsyncTask 完成时。
功能将是:
- 每个进度条都会随着下载过程而更新。
- 文件下载完成后会保存到 SD 卡中。
- 将添加它服务(在后台运行。
- 当下载一个文件时,它应该显示“完成”(不应删除表单列表视图)
我还使用了一个数据库来下载文件。该列表还取决于数据库向量。我该如何实现它。我也尝试过,但没有得到解决。
下面是两张截图来详细描述:
我也试过这个代码:
temp = databaseHandler.getAllNotifyNumber();
list_download = (ListView) findViewById(R.id.list_download);
myCustomAdapter = new MyCustomAdapter(mContext);
list_download.setAdapter(myCustomAdapter);
myCustomAdapter.notifyDataSetChanged();
for (int index = 0; index < temp.size(); index++) {
String url = "http://upload.wikimedia.org/wikipedia/commons/0/05/Sna_large.png";
grabURL(url);
}
这是 AsyncTask 方法:
public void grabURL(String url) {
new GrabURL().execute(url);
}
private class GrabURL extends AsyncTask<String, Integer, String> {
protected String doInBackground(String... urls) {
String filename = "MySampleFile.png";
File myFile = new File(directory, filename);
try {
URL url = new URL(urls[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileSize = connection.getContentLength();
InputStream is = new BufferedInputStream(url.openStream());
OutputStream os = new FileOutputStream(myFile);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = is.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100 / fileSize));
os.write(data, 0, count);
}
os.flush();
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return filename;
}
@Override
protected void onProgressUpdate(Integer... progress) {
// home_countprogress.setVisibility(View.VISIBLE);
// home_countprogress.setText(String.valueOf(progress[0]) + "%");
// progressbar_horizontal.setProgress(progress[0]);
myCustomAdapter.notifyDataSetChanged();
}
@Override
protected void onCancelled() {
Toast toast = Toast.makeText(getBaseContext(),
"Error connecting to Server", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
}
@Override
protected void onPostExecute(String filename) {
// progressbar_horizontal.setProgress(100);
// home_countprogress.setVisibility(View.VISIBLE);
}
protected void onPreExecute() {
// progressbar_horizontal.setVisibility(View.VISIBLE);
// progressbar_horizontal.setProgress(0);
// home_countprogress.setVisibility(View.VISIBLE);
myCustomAdapter = new MyCustomAdapter(mContext);
}
}
【问题讨论】:
-
您是否按照以下解决方案进行了这项工作。如果是这样,请让我知道,因为我有同样的实施问题。在我的情况下,还有一个下载所有按钮,它应该为每个列表视图项目同时显示多个旋转进度条。
标签: android android-listview android-asynctask download android-progressbar