ProgressBar不会停止

时间:2014-06-25 07:11:00

标签: android android-asynctask progressdialog

我已经创建了一个用户登录他的帐户的应用程序。为此我使用了asyncTask.Everything工作正常,但事情就是当我得到响应时我想要进度条停止。但是它去了持续不断。

异步任务

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 个答案:

答案 0 :(得分:6)

<强>问题:

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

答案 1 :(得分:1)

如果没有progressdialog show,问题就是你的创建实例。所以第二次

if (progressDialog.isShowing ()) 

以上条件是假的。

答案 2 :(得分: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();

答案 3 :(得分: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 ();
        }
    }
}

答案 4 :(得分: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);