进度条未显示

时间:2013-09-30 01:14:40

标签: android android-asynctask progress-bar

我正在尝试在更新数据库时使用进度条。好的,我可以成功更新我的数据库,但我的进度条没有显示。我正在使用进度条,它还会显示我更新的百分比。我不知道下面的代码有什么问题,请帮我解决一下:

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));
    }
}

}

1 个答案:

答案 0 :(得分:1)

我在这里看到几个问题。您Void

中的第二个参数有AsyncTask
public class SyncBrand extends AsyncTask<String, Void, Boolean>

这意味着onProgressUpdate()应该期望传递给它的数据类型,但是你有protected void onProgressUpdate(String... progress) {告诉该方法采用String参数。另外,您不会从publishProgress()拨打doInBackground(),这是用于呼叫onProgressUpdate()的内容。

AsyncTask更改为

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

并将publishProgress()添加到doInBackground()并传递您希望它将String更新为ProgressDialog的{​​{1}}值。