android - 进度条不显示

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

最佳答案

我在这里看到了几个问题。你有 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 的值到。

关于android - 进度条不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19085130/

相关文章:

c# - wpf C# 中的进度条

Android Activity堆栈和返回函数问题

android - 无法向服务器发送登录请求

Android:如何使用 Async 下载 .png 文件并将其设置为 ImageView?

c# - 使用后台工作程序 - 更新递归方法进度上的进度条

java - 如何在android中制作离散的搜索/进度条?

java.lang.NoClassDefFoundError,Android Studio 2.6.0,内部类,

android - 无法隐藏 SearchView 的虚拟键盘 ifiedbydefault(false)

java - ARCore – 禁用 ModelRenderable 的移动

android - 使用 AsyncTask 在 ListView 中加载图片