【问题标题】:why is on progress update not showing the right progress为什么进度更新没有显示正确的进度
【发布时间】:2014-09-12 03:35:06
【问题描述】:

我正在使用 AsyncTask 从数据库中获取数据。 为了通知用户下载了多少数据,我使用了 ProgressDialog 和 onProgressUpdated - 但它不会显示“正确”的进度。在我的情况下,它只显示“正在加载... 0%”

AsyncTask<>
{    
...
    @Override
    protected Integer doInBackground(String... params)
    {
        for (int i = 0; i < size; i++)
        {
          ...
          this.publishProgress(i, size);            
          Thread.sleep(500); // Just to make the Progress visible
        }
     }

    @Override
    protected void onProgressUpdate(Integer... values)
    {
        progressDialog.setMessage("Loading... " + values[0] / values[1]*100 + "%");
    }
}

你有什么建议吗?

谢谢

【问题讨论】:

  • 因为您正在执行整数值之间的除法。
  • 不,当然不会,但是 (int)((values[0]/(float)values[1]) * 100),可能会

标签: android asynchronous task progress


【解决方案1】:

试试这个 this.publishProgress(100 / (size - I));

在您的 onProgressUpdate 上执行此操作 progressDialog.setMessage("加载中..." + values [0]);

【讨论】:

  • 感谢您的建议 - 但 Blackbelt 已经有了解决方案 - 我有一个 integerdivison。 protected void onProgressUpdate(Float... values) 和 this.publishProgress((float)i, (float)size);解决了问题
猜你喜欢
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 2017-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
相关资源
最近更新 更多