7

I have asynktask that shows a progressDialog and update its value in doInBackground method. the methods code:

@Override
protected void onPreExecute() {
    progress = new ProgressDialog(cont);
    progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progress.setIndeterminate(true);
    progress.setTitle(cont.getResources().getString(R.string.pleaseWait));
    progress.setMessage(cont.getResources().getString(R.string.loadingImages));
    progress.show();
    super.onPreExecute();
}

@Override
protected String doInBackground(Void... arg0) {
    progress.setProgress(2);
    //do some work on the database and network
    progress.setProgress(25);
    //Do some extra work
    for(int i = 0; i < itemImagesList.size(); i++){
        publishProgress((int) ((i / (float) itemImagesList.size()) * 100));
        //Do somework
    }
}

@Override
protected void onProgressUpdate(Integer... prog) {
    progress.setProgress(prog[0]);
}

@Override
protected void onPostExecute(String result) {
    progress.dismiss();
    super.onPostExecute(result);
}

The progressDialog value doesn't change at all! and if I tried to set the dialog message using:

progress.setMessage("At item "+i);

some exception occurs if I put it in the middle of the method, but in the first it works fine!

what's wrong?

Muayad Salah
  • 701
  • 1
  • 8
  • 19
  • You just need to remove progress.setProgress(2);, progress.setProgress(25); these from the doInBackground, and you can see your progress. because update progress already updating at UI – IshRoid Mar 11 '15 at 10:02
  • Have you tried this ? http://stackoverflow.com/questions/7970113/change-a-dialogs-message-while-using-asynctask?answertab=active#tab-top – Jejefcgb Mar 11 '15 at 10:03

3 Answers3

16

Your issue is related to setIndeterminate(true); You should set it to false if you want to have progress update (take a look at Android setProgress doc), don't forget to also use setMax() to set progress max value to the desired one.

And as said Laurent you can change progress only on UI thread so you have to do it in onProgressUpdate and not in doInBackground

Gaëtan Maisse
  • 12,208
  • 9
  • 44
  • 47
2

You have to update progress value inside UI thread so in method onProgressUpdate of your AsyncTasK

LaurentY
  • 7,495
  • 3
  • 37
  • 55
1

Do it like this

@Override
        protected String doInBackground(Void... arg0) {

                    getActivity ().runOnUiThread (new Runnable() {

                    @Override
                    public void run () {

                          progress.setProgress(2);
                                       //do some work on the database and network
                          progress.setProgress(25);

                    }
                });
            //Do some extra work
            for(int i = 0; i < itemImagesList.size(); i++){
                publishProgress((int) ((i / (float) itemImagesList.size()) * 100));
                //Do somework
            }
        }
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83