【问题标题】:How to set download progress in a progress bar that is in a list view from thread?如何在线程列表视图中的进度条中设置下载进度?
【发布时间】:2014-03-13 10:31:34
【问题描述】:

我正在使用下载管理器下载文件。我想在列表视图中显示进度条,显示下载状态。我正在计算线程中的进度。当我尝试从线程设置进度时没有显示进度。我应该怎么做才能取得进展? 这是我的代码

public Runnable updateprogress = new Runnable() {
    public void run() {
        try {
            Log.v("updateprogress in thread",""+killthread);

            if(killthread==false)
            {
                DownloadManager downloadmanager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);

                DownloadManager.Query q = new DownloadManager.Query();
                q.setFilterById(download_id);
                Cursor cursor = downloadmanager.query(q);
                if (cursor.moveToFirst()) {

                    FileDownloading.setDownload_status(cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)));
                    long fileSize = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                    long bytesDL = cursor.getLong(cursor.getColumnIndex(DownloadManager
                            .COLUMN_BYTES_DOWNLOADED_SO_FAR));

                    int progress=(int) ((bytesDL * 100.0f) / fileSize);
                    FileDownloading.setDownload_progress((int) ((bytesDL * 100.0f) / fileSize));
                    if(fileSize!=-1){
                        PR_BAR.setIndeterminate(false);
                        FileDownloading.setDownload_progress(progress);
                        PR_BAR.setProgress((int) ((bytesDL * 100.0f) / fileSize));
                        if((int) ((bytesDL * 100.0f) / fileSize)==100){                             
                            killthread=true;
                            if(PR_BAR!=null)
                                PR_BAR.setVisibility(View.GONE);
                            else
                                Log.e("progress bar","null");

                        }

                    }
                    else{
                        PR_BAR.setIndeterminate(true);
                    }

                } else {

                    FileDownloading.setDownload_progress(0);
                    FileDownloading.setDownload_status(-1);
                }
                cursor.close();
                mHandler.postDelayed(this, 5000);
            }
            else{
                updateprogress.wait();
            }

        } catch (Exception e) {
        }
    }


};

【问题讨论】:

    标签: android multithreading listview progress-bar


    【解决方案1】:

    你应该使用 AsyncTask ,实现 onProgressUpdate()

    【讨论】:

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