在网格视图中显示视图之前,“进度”对话框将被取消

时间:2016-06-22 05:07:45

标签: android progressdialog

在启动我的Web请求调用之前,我在片段的oncreate方法中启动我的进度对话框。在Web请求调用中,如果我获取响应并且如果成功,则调用notifydatasetchanged方法来刷新适配器。但是在更新视图之前,对话框会被解雇。请帮忙 。

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    pd = ProgressDialog.show(getActivity(), "Loading...", "Please Wait...");
    getProducts(highPrice, lowPrice, isLowtoHigh);
}


 private void getProducts(String highPrice,String lowPrice,String isLowtoHigh) {

   // loadingDialog.loading();

    APIRequst.getProductsCategory(getActivity().getApplicationContext(), isLowtoHigh, lowPrice, highPrice, new APIRequestListner() {
        @Override
        public void onSuccess(String response) {

            if (response == null || response.isEmpty()) {
                Log.e("orderhistory", "success but empty");
            } else {
                Log.e("products", response);
                try {
                    JSONObject mainObj = new JSONObject(response);
                    boolean result = mainObj.has("is_success") && mainObj.getBoolean("is_success");

                    String resultMessage = mainObj.getString("message");
                    if (resultMessage.equalsIgnoreCase("Success")) {

                        if (result) {


                            productItems.clear();
                            adptProductItems.clear();
                            JSONArray jsonOrderList = mainObj.has("categories") ? mainObj.getJSONArray("categories") : null;
                            if (jsonOrderList != null) {
                                for (int i = 0; i < jsonOrderList.length(); i++) {
                                    JSONObject jsonObj = jsonOrderList.getJSONObject(i);
                                    ProductListItem newItem = (new Gson()).fromJson(jsonObj.toString(), ProductListItem.class);
                                    productItems.add(newItem);
                                }

                                adptProductItems.notifyDataSetChanged();
                                pd.dismiss();
                            }
                        }
                    } else {

                        if (resultMessage.equalsIgnoreCase("No Value")) {

                            if (pd.isShowing())
                                pd.dismiss();
                            productItems.clear();
                            adptProductItems.notifyDataSetChanged();

                            Toast.makeText(getActivity(), "Sorry no prducts to display", Toast.LENGTH_SHORT).show();
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            // adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure() {
            if (pd.isShowing())
                pd.dismiss();
            //  loadingDialog.dismissDialog();
        }
    });
}

1 个答案:

答案 0 :(得分:0)

尝试移动“pd.dismiss();”下面onFailure()

  @Override
        public void onFailure() {
            if (pd.isShowing())
                pd.dismiss();
            //  loadingDialog.dismissDialog();
        }
pd.dismiss();

 adptProductItems.notifyDataSetChanged();
                                //pd.dismiss(); remove fromhere

可能会像我在我的情况下那样有所帮助..