Pro*_*nto 11 android download android-listview android-progressbar android-asynctask
我们如何更新ListView中的进度条.当每个进度条与文件下载相关联时,通过AsyncTask完成.
该功能将是:
我还使用数据库下载文件.该列表还取决于数据库向量.我该如何实现它.我也试过但没有得到解决方案.
以下是详细描述它的两个屏幕截图:

我也试过这段代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
这是AsyncTask mathod:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试执行以下操作,而不是每次进度更改时都通知您的适配器(始终如此)并使其完全低效。
| 归档时间: |
|
| 查看次数: |
4357 次 |
| 最近记录: |