【问题标题】:Update progress bar in listview for multi files download更新列表视图中的进度条以下载多文件
【发布时间】:2014-01-12 17:07:42
【问题描述】:

我们如何更新 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);
}

这是 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


【解决方案1】:

不要使用 ListView 试试这个:

创建一个包含你的东西的布局 + ProgresseBar 它看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp">

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/image"
    android:visibility="gone" />

</LinearLayout>

在您的活动/片段的布局中,将您的 ListView 替换为具有与您的列表视图相同的属性的 LinearLayout(或 ScrollView 内的 LinearLayout):

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView

并在您的活动中创建一个函数:

private void addMyView(String url){
     LayoutInflater mInflater   = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View mView                 = mInflater.inflate(R.layout.my_layout, null, false);
     ProgressBar mBar           = (ProgressBar) mView.findViewById(R.id.progress);

     container.addView(mView);

     grabURL(url); // starting your task here

}

也不要忘记你的 LinearLayout :

LinearLayout container          = (LinearLayout) findViewById(R.id.container);

然后:

for (int index = 0; index < temp.size(); index++) {
    String url = "http://upload.wikimedia.org/wikipedia/commons/0/05/Sna_large.png";
    addMyView(url);`enter code here
}

希望有所帮助:)

【讨论】:

    【解决方案2】:

    您可以尝试执行以下操作,而不是每次您的进度发生变化时(一直如此)通知您的适配器并使其完全低效。

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

    【讨论】:

      猜你喜欢
      • 2011-10-27
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多