更新列表视图中的进度条以下载多个文件

Pro*_*nto 11 android download android-listview android-progressbar android-asynctask

我们如何更新ListView中的进度条.当每个进度条与文件下载相关联时,通过AsyncTask完成.

该功能将是:

  1. 每个进度条都将通过下载过程进行更新.
  2. 当文件完成下载后,它将保存到SD卡中.
  3. 它将添加它服务(在后台运行.
  4. 下载一个文件时,应显示"完成"(不应删除表单列表视图)

我还使用数据库下载文件.该列表还取决于数据库向量.我该如何实现它.我也试过但没有得到解决方案.

以下是详细描述它的两个屏幕截图:

在此输入图像描述

我也试过这段代码:

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)

Tas*_*orf 2

您可以尝试执行以下操作,而不是每次进度更改时都通知您的适配器(始终如此)并使其完全低效。

  • 为 ListView 中的每一行创建一个自定义视图
  • 添加一个名为“setData()”的方法或其他方法。在适配器的 getView() 回调中调用此方法,设置该视图的数据。
  • 在“setData()”内部将该视图注册为异步任务进度更新的侦听器。
  • 在自定义视图中覆盖 onStartTemporaryDetach() 和 onDetachedFromWindow() 。确保从那里取消注册异步任务更新(否则会出现内存泄漏)。
  • 在异步任务中,只需通知所有已注册的侦听器(这些侦听器可能保存在侦听器的 url 映射或类似内容中)。