for (int i = 0; i < userdataarray.length(); i++) {
JSONObject jsonobject = userdataarray.getJSONObject(i);
String lat = jsonobject.getString("lattitude");
String lon = jsonobject.getString("longitude");
la = Double.parseDouble(lat);
lo = Double.parseDouble(lon);
Log.i("LOG", "1");
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Log.i("LOG", "2");
mMap.addMarker(new MarkerOptions()
.position(new LatLng(la, lo))
.title(category));
}
});
The code snippet specified above is present in the onResponse() method of an okhttp call. Essentially, this for loop is being executed asynchronously. Now the problem I'm facing is that the mMap.addMarker method needs to be called for all the values of lat and long as the for loop progresses.
I have placed two Log.i() printers, one outside the UI updater and one inside. What I want is for the Logs to print alternatively:
1
2
1
2
1
2
but what's happening is
1
1
1
2
2
2
Is there any way to make it work the way I want to? Please forgive the noob code. Thanks in advance!