2014-06-25 88 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);