onInfoWindowClick最终id只传递volley数组的最后一个值

时间:2016-04-15 23:49:18

标签: android arrays infowindow

我试图将来自凌空数组的ID传递给onInfoWindowClick并将其放在捆绑包上以打开新的详细活动(Dettaglio)。 为了做到这一点,它说我必须做出最终的ID。如果我这样做,地图打开,显示所有标记,但如果我打开一个infowondow并单击每个窗口只打开数组的最后一个ID,并在细节关闭couerse它显示相同的详细记录。 目前我安排将我需要的ID作为标题,以便我可以毫无问题地阅读它并将其传递给捆绑包。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{

    private GoogleMap mMap;
    RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        Bundle bundle = getIntent().getExtras();
        Double lat = bundle.getDouble("lat");
        Double lng = bundle.getDouble("lon");
        Toast.makeText(getApplicationContext(), "La tua posizione: " + lat + ", " + lng, Toast.LENGTH_LONG).show();

        LatLng centra = new LatLng(lat,lng);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(centra, 11));

        requestQueue = Volley.newRequestQueue(this);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://xxx.it/jasone_coords.php?lat="+lat+"&lng="+lng,

                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("serv_response");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject risultati = jsonArray.getJSONObject(i);
                                //final String id = risultati.getString("id");
                                String id = risultati.getString("id");
                                String locale = risultati.getString("locale");
                                //String indirizzo = risultati.getString("indirizzo");
                                //String citta = risultati.getString("citta");
                                Double lat = risultati.getDouble("lat");
                                Double lng = risultati.getDouble("lng");

                                LatLng p1 = new LatLng(lat,lng);
                                mMap.addMarker(new MarkerOptions().position(p1).title(id).snippet(locale));

                                    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

                                        @Override
                                        public void onInfoWindowClick(Marker marker) {
                                            Toast.makeText(getApplicationContext(), marker.getSnippet(), Toast.LENGTH_LONG).show();
                                            Intent i = new Intent(getApplicationContext(), Dettaglio.class);
                                            Bundle bundle = new Bundle();
                                            //bundle.putString("stuff", id);/// it does not work
                                            bundle.putString("stuff", marker.getTitle());// is dirt but works
                                            i.putExtras(bundle);
                                            startActivity(i);
                                        }
                                    });

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("VOLLEY", "ERROR");
                    }
                }
        );
        requestQueue.add(jsonObjectRequest);

    }
}

抱歉无知,我只是学习! 谢谢!!!

0 个答案:

没有答案