2

i have code like this but why i can't see the progressdialog?

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

and i try to remove some code so the code like this

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

and progressdialog is coming show

so how to show progressdialog with my some code like above?

Eggy
  • 522
  • 5
  • 29
  • Where did you call progress.show() in your code?? And can you post ProgressDialog.show()? – Saritha G Jul 15 '16 at 06:45
  • short msg : Show your `ProgressDialog` in `PreExecuted` method and hide it `onPostExecute` method. – Harshad Pansuriya Jul 15 '16 at 06:50
  • it's my code `progress = ProgressDialog.show(MainActivity.this, "MOHON DITUNGGU", "Loading...", true);` it's will show progressdialog but if there's not following code, if have following code that will not show – Eggy Jul 15 '16 at 06:52
  • Are you using an `ASyncTask` for this? If so, go with @Ironman's proposal – 0xDEADC0DE Jul 15 '16 at 06:57

3 Answers3

1

Simply there's nothing wrong, i guess your loop execution happening so quickly that your progress dialog being dismissed even before u can see it

OR

Some exception occurred in your try block and dialog gets dismissed in catch block

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

Every thing is fine you just have to add some delay in thread.

Recommened: try to use AsyncTask for this type of work.

   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); 
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • Oh yeah it's work!! but why when first progressdialog show the circle loading is run, but after that the circle loading is stuck? – Eggy Jul 15 '16 at 07:09
  • make sure you have `progress.dismisse();` in your code. – Sohail Zahid Jul 15 '16 at 07:12
  • yes i have `progress.dismiss();` . over all it's work, the problem just animated of circle loading it's just animated for maybe 1 second, after that the animated is stop – Eggy Jul 15 '16 at 07:17
  • ahaaa its because everything is happening in UI Thread as i recommended use AsyncTask for this type of work. – Sohail Zahid Jul 15 '16 at 07:18
  • if using AsyncTask, i must still using your code (handler,etc) or not? – Eggy Jul 15 '16 at 07:23
  • @E-Place look here http://stackoverflow.com/questions/24399294/android-asynctask-to-make-an-http-get-request – Sohail Zahid Jul 15 '16 at 07:31
0

INitialize progress Dialog like this

  ProgressDialog  myProgressDialog = new ProgressDialog(this);
    myProgressDialog.setIndeterminate(true);
    myProgressDialog.setMessage("Loading...");
    myProgressDialog.setCancelable(true);

and to show progress dialog write

myProgressDialog.show();

to dismiss

myProgressDialog.dismiss();
Manohar
  • 22,116
  • 9
  • 108
  • 144