ProgressDialog未在Async任务中更新

时间:2014-04-02 10:26:42

标签: android

有人解释为什么我的异步任务运行时我的进度对话框没有更新吗? 将出现进度对话框,但百分比指示器根本不会增加。 这里有什么明显的东西吗?

private class DownloadFilesTask extends AsyncTask<ArrayList<String>, Integer, Long> {
        private ProgressDialog mProgress;

        @Override
        protected  void onPreExecute()
        {
            mProgress = new ProgressDialog(HelloActivity.this);
            mProgress.setMessage("Fetching Image data..");
            mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgress.setCancelable(false);
            //mProgress.setIndeterminate(false);
            mProgress.show();
        }
        @Override
        protected Long doInBackground(ArrayList<String>... urls) {

            long totalSize = 0;

            for(int i=0; i < dbLinks.size(); i++){

                try {
                    ....doe some stuff here
                    publishProgress((i/dbLinks.size()) * 100);
                } catch (DbxException e) {
                    e.printStackTrace();
                }
            }

            return totalSize;
        }
        @Override
        protected void onProgressUpdate(Integer... progress) {

            mProgress.setProgress(progress[0]);

        }
        @Override
        protected void onPostExecute(Long result) {

            mProgress.dismiss();
            //use totalsize if necessary
            Intent intentFB = new Intent(HelloActivity.this, PostActivity.class);
            intentFB.putExtra("key", dbURLs);
            startActivity(intentFB);
        }
    }

2 个答案:

答案 0 :(得分:1)

整数数学。

(i/dbLinks.size()) * 100

i/dbLinks.size()将始终为0并将其乘以100并不会更改它。

要解决此问题,请考虑将进度条最大值设置为dbLinks.size()并将i发布为进度。

答案 1 :(得分:0)

您可能在doInBackground方法中获得异常,因为我检查了示例演示它是否正常工作。

private class DownloadFilesTask extends AsyncTask<String, Integer, Long> {
    private ProgressDialog mProgress;

    @Override
    protected  void onPreExecute()
    {
        mProgress = new ProgressDialog(MainActivity.this);
        mProgress.setMessage("Fetching Image data..");
        mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgress.setCancelable(false);
        //mProgress.setIndeterminate(false);
        mProgress.show();
    }
    @Override
    protected Long doInBackground(String... progress) {

        long totalSize = 0;

        for(int i=0; i < 100; i++){

            try {

                publishProgress(i);
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return totalSize;
    }
    @Override
    protected void onProgressUpdate(Integer... progress) {

        mProgress.setProgress(progress[0]);

    }
    @Override
    protected void onPostExecute(Long result) {

        mProgress.dismiss();
    }
}