首页 > 解决方案 > 无法关闭进度默认进度对话框andorid

问题描述

我是新手,Android我自己学习。我正在尝试实现android的默认值ProgressDialog,但问题是,当我dismiss()在网络响应中调用方法时,它并没有消失。

private void loadData() {
//        rooms.clear();
        final ProgressDialog progress = new ProgressDialog(getContext());
        progress.show(getContext(),"Loading..","Please wait");
        progress.show();    
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Api.getAllPost, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                progress.dismiss();
                try {
                    JSONArray array = response.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object = array.getJSONObject(i);
                        Room room = new Gson().fromJson(object.toString(),Room.class);
//                        Toast.makeText(context, "Sucess", Toast.LENGTH_SHORT).show();
                        rooms.add(room);
                    }
                    roomAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progress.dismiss();
                Toast.makeText(context, "Server error", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

我在这个网站上尝试了其他解决方案,但没有奏效。请有人帮助我。

标签: androidandroid-studio

解决方案


首先不要使用ProgressDialog,因为它现在已被弃用。但是对于您的解决方案,请尝试替换您的代码:

final ProgressDialog progress = new ProgressDialog(getContext());
progress.show(getContext(),"Loading..","Please wait");
progress.show();

通过以下代码:

final ProgressDialog progress = new ProgressDialog(getContext());
progress.setTitle("Loading..");
progress.setMessage("Please wait");
progress.setCancelable(false);
progress.show();

它应该工作:)


推荐阅读