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