【问题标题】:ProgressBar doesnt stopsProgressBar 不会停止
【发布时间】:2014-06-25 07:10:59
【问题描述】:

我已经创建了一个应用程序,其中用户在他的帐户中登录。为此,我使用了 asyncTask。一切正常,但问题是当我得到响应时,我希望进度条停止。但它会继续进行。

异步任务

protected void onPreExecute() {
        super.onPreExecute ();
        CommonFunctions.showProgress (c, "Please Wait", true);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute (s);
        try {
            JSONObject jsonObject = new JSONObject (s.trim ());
            JSONObject NewDataSet = jsonObject.getJSONObject ("NewDataSet");
            JSONObject Table = NewDataSet.getJSONObject ("Table");
            String User_ID = Table.getString ("User_ID");
            String Vendor_IEntity_Code = Table.getString ("Vendor_IEntity_Code");
            String Vendor_Name = Table.getString ("Vendor_Name");


            // setting the preferences
            SettingPreference.setUserId (c, User_ID);
            SettingPreference.setVendorId (c, Vendor_IEntity_Code);
            SettingPreference.setVendorName (c, Vendor_Name);

        } catch (JSONException e) {
            e.printStackTrace ();
        }
        CommonFunctions.showProgress (c, "", false);

        Crouton.makeText ((android.app.Activity) c, "Login Sucessful", Style.CONFIRM).show ();

    }

    @Override
    protected String doInBackground(String... strings) {
        response = HttpRequest.post ("https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/IsUserValid").send ("_UserID=" + strings[0] + "&_Password=" + strings[1]).body ();
        Log.e ("Login Response", "" + response);
        return response;
    }  

常用函数

public class CommonFunctions {

    private Context c;


    public static void showProgress(Context context, String message, boolean isVisible) {

        ProgressDialog progressDialog = new ProgressDialog (context);
        progressDialog.setMessage (message);
        progressDialog.setCancelable (false);
        if (isVisible) {
            progressDialog.show ();
        } else if (isVisible == false) {
            if (progressDialog.isShowing ()) {
                progressDialog.dismiss ();
            }
        }
    }
}

【问题讨论】:

    标签: android android-asynctask progressdialog


    【解决方案1】:

    问题:

    ProgressDialog progressDialog = new ProgressDialog (context);
    

    因此,每次调用 showProgress 方法时,您都会创建一个新的 ProgressDialog,因此在再次调用该方法时它不会关闭。

    解决方案:

    只创建一次ProgressDialog实例

    public class CommonFunctions {
    
        private Context c;
        ProgressDialog progressDialog;
    
    
        public static void showProgress(Context context, String message, boolean isVisible) {
    
            if(progressDialog == null)
            {
                 progressDialog = new ProgressDialog (context);
                 progressDialog.setMessage (message);
                 progressDialog.setCancelable (false);
            }
    
            if (isVisible) {
                progressDialog.show();
            } else if (isVisible == false) {
                if (progressDialog.isShowing ()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
           }
        }
    }
    

    【讨论】:

    • @user3667909 欢迎您 +1 并为这篇文章投票,如果它有帮助,谢谢。
    • @user3667909 如果它对您有用,您可以接受答案。从我这边 +1
    【解决方案2】:

    问题是您在没有progressdialog 显示的情况下再次创建实例。所以第二次

    if (progressDialog.isShowing ()) 
    

    以上条件为假。

    【讨论】:

      【解决方案3】:

      我建议您使用这种方法,因为那里提供这些方法是有原因的。 在onPreExecute() 中启动您的进度条,然后在onPostexecute() 中停止它。

      new AsyncTask<Void, Void, Void>() {
      
      ProgressDialog dialog;
      
            protected void onPreExecute() {
             dialog = new ProgressDialog(MyActivity.this);
             dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
             dialog.setMessage("Your Message");
             dialog.setIndeterminate(true);
             dialog.setCancelable(false);
             dialog.show();
            };
      
            @Override
            protected Void doInBackground(Void... params) {
              // Your Code        
              return null;
            }
      
            protected void onPostExecute(Void result) {
             dialog.dismiss();
             // UI updates if any
            };
      
      }.executeOnExecutor();
      

      【讨论】:

        【解决方案4】:

        试试这个方法,希望能帮助你解决问题。

        public class CommonFunctions {
        
            private static ProgressDialog progressDialog;
        
            public static void showProgress(Context context, String message, boolean isVisible) {
        
                if(progressDialog == null){
                    progressDialog = new ProgressDialog (context);
                    progressDialog.setMessage (message);
                    progressDialog.setCancelable (false);
                }
                if (isVisible) {
                    progressDialog.show ();
                }else{
                    progressDialog.dismiss ();
                }
            }
        }
        

        【讨论】:

          【解决方案5】:

          在 showProgress() 中,您正在创建新对象。因此,当您调用此方法来隐藏进度条时,它正在创建新对象并隐藏新对象而不是前一个对象。

          您需要更新 CommonFunctions 类如下。

          public class CommonFunctions {
          
              private Context c;
              ProgressDialog progressDialog;
          
              public CommonFunctions(Context context){
                this.c = context;
                progressDialog = new ProgressDialog (context);
              }
          
              public static void showProgress(String message, boolean isVisible) {
                      progressDialog.setMessage (message);
                  progressDialog.setCancelable (false);
                  if (isVisible) {
                      progressDialog.show ();
                  } else if (isVisible == false) {
                      if (progressDialog.isShowing ()) {
                          progressDialog.dismiss ();
                      }
                  }
              }
          }
          

          如下使用:

           CommonFunctions cf = new CommonFunctions(context);
          

          显示进度使用以下:

          cf.("Please Wait", true);
          

          隐藏进度使用以下:

          cf.("", false);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-05
            相关资源
            最近更新 更多