Issue about for loop in android

158 views Asked by At

I am make my final project but am get stuck . I have a List and a for loop like code below

final List<Results> ListStadium=response.body().getResults();
                for( int i=0;i<ListStadium.size();i++)
                {
                    final Double latStadium,longStadium;
                    final String nameStadium;
                    latStadium = Double.parseDouble(ListStadium.get(i).getLatitude()); // *1
                    longStadium=Double.parseDouble(ListStadium.get(i).getLongitude()); //*2
                    nameStadium=ListStadium.get(i).getName();  //*3

                    //////////////////////////////////////////////

                    mapFragment.getMapAsync(new OnMapReadyCallback() {
                        @Override
                        public void onMapReady(final GoogleMap googleMap) {
                            googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                            googleMap.getUiSettings().setZoomGesturesEnabled(true);


                            gpsTracker = new GPSTracker(UserDashboard.this);

                            // check if GPS enabled
                            if(gpsTracker.canGetLocation()){

                                double latitude = gpsTracker.getLatitude();
                                double longitude = gpsTracker.getLongitude();
                                Log.e("Long: ",longitude+"");
                                Log.e("Lat: ",latitude+"");
                                googleMap.addMarker(new MarkerOptions()
                                        .position(new LatLng(latStadium,longStadium))
                                        .title(nameStadium)
                                        .snippet(GlobalVariable.Stadium_ID)
                                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
                                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(10.7886087,106.6974835), 10));
                                googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                                    @Override
                                    public boolean onMarkerClick(Marker marker) {
                                        GlobalVariable.Stadium_ID=marker.getSnippet();
                                        Toast.makeText(getApplicationContext(), marker.getTitle()+ " Here !", Toast.LENGTH_LONG).show();
                                        Intent intent = new Intent(UserDashboard.this,StadiumDetail.class);
                                        startActivity(intent);
                                        return false;
                                    }

                                });

                                progressDialog.dismiss();
                            }else{
                                // can't get location
                                // GPS or Network is not enabled
                                // Ask user to enable GPS/network in settings
                                gpsTracker.showSettingsAlert();
                            }

                        }
                    });
                }

But when run this loop when the program execute done *1 *2 *3 it auto increase i and not run mapFragment.getMapAsync() . Who can help me ?

2

There are 2 answers

0
Sean Amos On

You should not call mapFragment.getMapAsync in a loop. This is an asynchronous operation, which means that another thread will load the map and notify you via your callback when it is loaded.

You should load your map, wait for the callback, and then create your markers in a for-loop inside that callback.

0
dsapandora On

I will recommend that you create a helper class that create the marks, send the result of the method to that help and wait for the response of the asynchronous call.

Or you can use AsyncTask then If you are dependent on the result of an AsyncTask, you can do this. Object result = asyncTask.execute().get(); The type of the result is the return type in your doInBackground() method. But then your main thread will be waiting until the task is complete.