我在更新进度条时遇到问题。在这里,我制作了一个自定义进度条功能,如下所示
ProgressDialog progressBar1;
private int progressBarStatus = 0;
private void customProgressBar(){
progressBar1 = new ProgressDialog(DictionaryActivity.this);
progressBar1.setCancelable(true);
progressBar1.setMessage("Downloading File...");
progressBar1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar1.setMax(100);
progressBar1.setProgress(0);
progressBar1.show();
//reset progress bar status
progressBarStatus = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
Log.d("TAG", "run: pStatus" + " " + pStatus);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
// call progressBar status data update
progressBarStatus = setProgressBarStatus();
new Handler(Looper.getMainLooper()).post(new Runnable(){
@Override
public void run() {
progressBar1.setProgress(progressBarStatus);
Log.d("TAG", "run: handler: " +progressBarStatus);
}
});
}
// when, file is downloaded 100%,
if (progressBarStatus >= 100) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar1.dismiss();
}
}
}).start();
}
public int setProgressBarStatus(){
return progressBarStatus += 2;
}
然后从数据下载函数中调用它。数据下载完成后,我将数据插入 SQLite DB(本地 dB)。
在此处插入数据之前更新进度条。但是当数据声明插入进度条时不会更新。如何解决这个问题?谢谢大家。