android - 为什么进度对话框无法显示?

标签 android

我有这样的代码,但为什么我看不到进度对话框?

progress = ProgressDialog.show(MainActivity.this, "MOHON DITUNGGU", "Loading...", true);
String urlATM = "http://eplace.hol.es/android/OutputATM.php";
        try {
            JSONArray data = new JSONArray(getJSONUrl(urlATM));
            if(data.length()!=0){
                HashMap<String, String> map;

                for (int i = 0; i < data.length(); i++) {
                    JSONObject c = data.getJSONObject(i);
                    map = new HashMap<String, String>();
                    map.put("id_atm", c.getString("id_atm"));
                    map.put("id_bank", c.getString("id_bank"));
                    map.put("nama_bank", c.getString("nama_bank"));
                    map.put("latitude", c.getString("latitude"));
                    map.put("longitude", c.getString("longitude"));
                    map.put("name", c.getString("name"));
                    map.put("email", c.getString("email"));
                    MyArrListATM.add(map);
                    //end add marker

                    maps.addMarker(new MarkerOptions()
                            .title("ATM"+MyArrListATM.get(i).get("nama_bank"))
                            .snippet("24 Jam")
                            .position(new LatLng(
                                    Double.parseDouble(MyArrListATM.get(i).get("latitude")),
                                    Double.parseDouble(MyArrListATM.get(i).get("longitude")))));
                }
                progress.dismiss();
            }



        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

            progress.dismiss();

            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
            alertDialog.setTitle("PERINGATAN");
            alertDialog.setMessage("Error: "+e.getMessage());
            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "TUTUP", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            alertDialog.show();
        }

我尝试删除一些代码,所以代码如下

progress = ProgressDialog.show(MainActivity.this, "MOHON DITUNGGU", "Loading...", true);

进度对话框即将显示

那么如何使用上面的代码显示进度对话框?

最佳答案

一切都很好,你只需在线程中添加一些延迟即可。

推荐:尝试使用 AsyncTask 来完成此类工作。

   progress = ProgressDialog.show(MainActivity.this, "MOHON DITUNGGU", "Loading...", true);
   // Execute some code after 1 seconds have passed
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() {
         @Override 
         public void run() { 
              // YOur Request code here?

         } 
    }, 1000); 

关于android - 为什么进度对话框无法显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38389396/

相关文章:

android - 传输数据 USB

Android Layout 文字在左,图片在右

android - DatePicker 天数被截断

java - 获取 Android 可扩展列表中 View 的唯一标识符

Android POST 请求不起作用

android - sqlite 使用表中的额外字段升级数据库并保留旧数据

android - 运行时权限: no permission immediately after granting

java - SetExactAndAllowWhileIdle 不唤醒设备

Android CustomWebChromeClient openFileChooser 在上传前调整图像大小

android - 在当前主题中找不到 '@attr/shapeAppearanceSmallComponent'