2014-06-25 92 views
0

我已經做了一個應用程序,在其帳戶中的用戶日誌。對於這個我已經使用asyncTask.Everything工作正常,但事情是當我得到的響應我希望進度條停止。但它繼續汽車無。ProgressBar不會停止

異步任務

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

CommonFunctions

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

回答

5

問題:

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; 
      } 
     } 
    } 
} 
+0

三江源它的工作如此之多: ) – Anuj

+0

@ user3667909歡迎您+1,如果有幫助謝謝,請點擊此處。 –

+2

@Rod_Algonquin也+1我從... –

1

問題是你不progressdialog再次顯示創建實例。所以第二次

if (progressDialog.isShowing()) 

上述情況是錯誤的。

+0

羅仍然沒有工作 – Anuj

1

我會建議你使用這種方法只有一次例如,由於有原因只要有這些方法。 在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(); 
1

試試這種方式,希望這會幫助您解決您的問題。

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

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