【问题标题】:Progress Dialog Not Dismissed进度对话框未关闭
【发布时间】:2020-03-26 19:44:56
【问题描述】:

我正在开发可以执行 CRUD 的目录项目。在每个 CRUD 进程中,它都会显示 ProgressDialog。

问题是“更新数据目录”没有被解除并永远运行,直到 MJ 忘记如何说唱。我不明白为什么。请帮忙

代码

public void updateProduct(final String id, final String name, final String price, final String category, final String description, @Nullable String imageBase64) {
    Log.i("zihad", "updateProduct()");
    progressDialog = ProgressDialog.show(context, "Update data catalog", "Please wait ...");
    StringRequest myStringRequest = new StringRequest(Request.Method.POST, MainActivity.URL_SERVER+"/updateproduct.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String jsonResponse) {
                    progressDialog.dismiss();
                    Log.i("zihad", "updateProduct().onResponse()");
                    try {
                        JSONObject jObject = new JSONObject(jsonResponse);
                        if (jObject.getBoolean("success")) {
                            Toast.makeText(context, "Product has been updated", Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            myDefaultErrorListener) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> mapParam = new HashMap<>();
            mapParam.put("id", id);
            mapParam.put("name", name);
            mapParam.put("price", price);
            mapParam.put("category", category);
            mapParam.put("description", description);

            return mapParam;
        }
    };
    requestQueue.add(myStringRequest);

    if (imageBase64 != null) {uploadImageProduct(id, imageBase64);}
}

public void uploadImageProduct(final String id, final String imageBase64) {
    Log.i("zihad", "uploadImageProduct()");
    progressDialog = ProgressDialog.show(context, "Upload image catalog", "Please wait ...");
    StringRequest myStringRequest = new StringRequest(Request.Method.POST, MainActivity.URL_SERVER+"/uploadimage.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String jsonResponse) {
                    Log.i("zihad", "uploadImageProduct().onResponse()");
                    progressDialog.dismiss();
                }
            },
            myDefaultErrorListener) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> mapParam = new HashMap<>();
            mapParam.put("id", id);
            mapParam.put("imageBase64", imageBase64);

            return mapParam;
        }
    };
    requestQueue.add(myStringRequest);
}

日志截图

【问题讨论】:

  • 尝试改用progressDialog.cancel();
  • 您使用相同的变量 - progressDialog - 用于 ProgressDialog 的两个不同实例,并且它们被混淆了。按照代码旁边的日志。 uploadImageProduct() 运行后,progressDialog 引用“上传图像目录”实例。
  • @MikeM。正确答案

标签: android android-volley progressdialog


【解决方案1】:

尝试将您的进度对话框设置为确定。在你的代码之后

progressDialog = ProgressDialog.show(context, "Update data catalog", "Please wait ...");

添加这一行:

progressDialog.setIndeterminate(false);

【讨论】:

    【解决方案2】:
    public static ProgressDialog show(Context context, CharSequence title,
            CharSequence message, boolean indeterminate,
            boolean cancelable, OnCancelListener cancelListener) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setIndeterminate(indeterminate);
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.show(); // show the dialog
        return dialog;
    }
    

    回调方法 onResponse 不会被调用,直到它们来自您的服务器的响应。

    show() 方法将为您创建并显示一个对话框,因此如果您调用该方法两次而不关闭第一个,第一个将不会永远关闭,因为该对话框的引用不会被使用。

    【讨论】:

      【解决方案3】:
       final viewProgressDialogue viewProgressDialogue = new viewProgressDialogue(DebugActivity.this);
          viewProgressDialogue.show();
          new Handler().postDelayed(new Runnable() {
              @Override
              public void run() {
                  Log.d("response","im calling");
                  viewProgressDialogue.dismiss();
              }
          }, 5000);
      

      【讨论】:

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