lolliloop
lolliloop

Reputation: 399

progress bar is not showing

I am trying to use progress bar while updating my database. Good thing, i can successfully update my database but my progress bar is not showing. I am using a progress bar the will show also the percentage on my update. I don't know what wrong with my code below, help me figure it out please:

public class SyncBrand extends AsyncTask<String, Void, Boolean>
{
    public static final int BRAND_DIALOG_DOWNLOAD_PROGRESS = 0;

public SyncBrand(Context context, String _username, String _password, String _code,String _remarks,String _date,String _province,String _infotype,
        String _competitor,ArrayList<String> _brands, ArrayList<String> _segments) 
{
    ....
}

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case BRAND_DIALOG_DOWNLOAD_PROGRESS:
        progressDialog = new ProgressDialog(mContext);
        progressDialog.setMessage("Updating Sub Brands..");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setCancelable(false);
        progressDialog.show();
        return progressDialog;
    default:
        return null;
    }
}

@SuppressWarnings("deprecation")
protected void onPreExecute() 
{     
    super.onPreExecute();
    ((Activity) mContext).showDialog(BRAND_DIALOG_DOWNLOAD_PROGRESS);
}

protected Boolean doInBackground(String... arg0) 
{
    try{
        ....

    }catch (Exception e){
        Log.e("Update SubBrand", "Error:", e);
        exception = e;
        return false;
        }
    ....

    return true;
}

protected void onProgressUpdate(String... progress) {
    Log.d("ANDRO_ASYNC",progress[0]);
    progressDialog.setProgress(Integer.parseInt(progress[0]));

}

@SuppressWarnings("deprecation")
protected void onPostExecute(Boolean valid)
{

    ((Activity) mContext).removeDialog(BRAND_DIALOG_DOWNLOAD_PROGRESS);
    if(valid){

        .....

    }else{
        Toast.makeText(mContext, "Failed to update.Please try again.", Toast.LENGTH_SHORT).show();
        mContext.startActivity(new Intent(mContext, S_2nd_Main.class));
    }
}

}

Upvotes: 1

Views: 738

Answers (1)

codeMagic
codeMagic

Reputation: 44571

A couple problems I see here. You have Void for the second argument in your AsyncTask

public class SyncBrand extends AsyncTask<String, Void, Boolean>

which means that onProgressUpdate() should expect that type of data passed to it but you have protected void onProgressUpdate(String... progress) { which tells that method to take a String parameter. Also, you don't call publishProgress() from doInBackground() which is what is used to call onProgressUpdate().

Change your AsyncTask to

public class SyncBrand extends AsyncTask<String, String, Boolean>

and add publishProgress() to doInBackground() and pass the String value you want it to update the ProgressDialog to.

Upvotes: 1

Related Questions