1

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

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

}

4

1 回答 1

1

我在这里看到了几个问题。你有你Void的第二个论点AsyncTask

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

这意味着onProgressUpdate()应该期望传递给它的数据类型,但你有protected void onProgressUpdate(String... progress) {告诉该方法接受String参数。此外,您不会调用publishProgress()from doInBackground()which is what is used to call onProgressUpdate()

改变你AsyncTask

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

并添加publishProgress()doInBackground()传递String您希望它更新的值ProgressDialog

于 2013-09-30T02:37:26.827 回答