简体   繁体   中英

Java: array not receiving values

Here's the code. list is a global variable.

    ArrayList<LatLng> list = new ArrayList<LatLng>();

    public void addCoordinate(double a, double b){
        list.add(new LatLng(a,b));
    }

public ArrayList<LatLng> readItems(int ignore) throws JSONException {
    addCoordinate(2.0,2.0);


    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
            showUrl, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            System.out.println(response.toString());
            try {
                JSONArray records = response.getJSONArray("locations");
                for (int i = 0; i < records.length(); i++) {
                    JSONObject locations = records.getJSONObject(i);

                    double latitude = locations.getDouble("Latitude");
                    double longitude = locations.getDouble("Longitude");
                   // double lat = Double.parseDouble(latitude);
                   // double lng = Double.parseDouble(longitude);
                    list.add(new LatLng(latitude,longitude));
                }
                addCoordinate(3.0,3.0);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.append(error.getMessage());

        }
    });
    //requestQueue.add(jsonObjectRequest);

    addCoordinate(1.0,1.0);
    return list;
}

It only returns the two values when getCoordinate is called, (the 1,1 and the 2,2) but it skips over the (3,3). If anyone has a fix I would appreciate it thanks.

(3,3)仅在调用onResponse之后才会添加。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM