I have a problem with updating progressbar. Here I make a custom progressbar function like below
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;
}
Then call it from data downloaded function. When data download complete I insert data to SQLite DB (local dB).
Here before data insert strat progress bar update. But when data stated to insert progress bar not update. How can solve this problem? Thanks, everyone.
I have solved this problem. After 3 hour of research, I found that when I add insert function (from where I call DB to insert) in a Thread
and send The updated progress value to the Progress Thread
. Then it's worked perfectly and setProgress()
update the progress.
int pStatus = 0;
public void dataInsertinoUsingThread(final Example data){
new Thread(new Runnable() {
@Override
public void run() {
for (int i=0; i<data.getData().getBanglaWordDictionary().size(); i++){
.........
pStatus+=i;
}
}
}).start();
}
Thanks to everyone. If anyone has this type of problem. You can follow this way. Also, If anyone finds a better solution than my solution you can give your answer or suggestions.