简体   繁体   中英

Get value from class to activity?

Can anyone help. When I want to get temperature in another activity I get a value of 0. In onResponse I tried set tempreture and it was set. I dont know wheres the problem.

    private double temperature;

public double getTemperature() {
    return temperature;
}

public JSONparser(Context context, String city) {

    RequestQueue queue = Volley.newRequestQueue(context);
    String url = "http://api.openweathermap.org/data/2.5/weather?q=" + city;

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(
            Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    // TODO Auto-generated method stub
                    try {

                        temperature = (response.getJSONObject("main")
                                .getDouble("temp"));

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

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub

                }
            });

    queue.add(jsObjRequest);

}

Use singleton for your class object and also meke setFunction and not initialize directly the value becouse that value will be 0 if you don't use the same object

public void setTemperature(double mTemp) {
    temperature =  mTemp;
}

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