Reputation: 11824
I have a code in my android project which retrieves data using volley. I have an onclick item. What I want is, when I click the item:
1) The volley should get the data from API and after that 2) the startactivity() line executes.
But what is happening is that the startactivity() executes before volley completes.
My question is: what can be done to delay startactivity() so that volley gets the data first?
Here is my code:
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
final String str = (String) adapterView.getItemAtPosition(position);
String url1 = "https://maps.googleapis.com/maps/api/place/details/json?placeid="+placeids.get(position)+"&key="+browserKey;
JsonObjectRequest jsonObjReq2 = new JsonObjectRequest(url1, null, new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response) {
try {
String latitude = response.getJSONObject("result").getJSONObject("geometry").getJSONObject("location").getString("lat");
String longitude = response.getJSONObject("result").getJSONObject("geometry").getJSONObject("location").getString("lng");
editor2.putString("udupitown_endingpointlat_1", latitude);
editor2.putString("udupitown_endingpointlon_1", longitude);
editor2.putString("udupitown_endingpointplacename_1", str);
editor2.commit();
}
catch (Exception e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
MyApplication.getInstance().addToReqQueue(jsonObjReq2, "jreq2");*/
Intent m = new Intent(view.getContext(), VolleyMapviewerinside.class );
startActivity(m);
}
Upvotes: 0
Views: 317
Reputation: 132992
volley to get the data from api and after that startactivity() line executes.
Then instead of calling startActivity
just before adding request to addToReqQueue
, start Activity inside onResponse
method Response.Listener
:
@Override
public void onResponse(JSONObject response) {
// 1. save data in SharedPreferences
// call startActivity for starting VolleyMapviewerinside Activity
}
Upvotes: 3