I have 3 markers in a map. I get marker data through a server and associate it through each marker. Due to data amount, I only get a subset of data for each marker (for development I have 3 markers, but there may be many more). So I associate an ID to each marker, and for the next activity, I get that ID and retrieve the complete data set for the next activity to be displayed.
Unfortunately all markers end up being associated with the same ID. What am I missing here?
Initially I was passing a final
variable to the inner class in order to assign the ID. Finding this question: How to pass parameters to anonymous class? (kudos!) I introduced an initializer to the inner class, thinking the final
attribute might have been the problem. But nope. Still getting the same ID on all markers.
private class Getter extends AsyncTask<String, Void, JSONArray> {
private Exception exception;
protected static final String TAG = "Getter";
protected JSONArray doInBackground(String... urls) {
try {
try {
//get JSON data from server
return json_objects.getJSONArray("objects");
} catch (JSONException jse) {
//more error handling
}
}catch(Exception e) {
this.exception = e;
return null;
}
}
protected void onPostExecute(JSONArray obj_arr) {
try {
if (this.exception != null) {
//some error handling
} else if (obj_arr != null) {
for (int i = 0; i < obj_arr.length(); i++) {
JSONObject obj = obj_arr.getJSONObject(i);
create_marker(obj);
}
}
} catch(JSONException jse) {
//error handling
}
}
private void create_marker(JSONObject obj) {
try {
JSONObject location = obj.getJSONObject("location");
double lat = location.getDouble("lat");
double lng = location.getDouble("lng");
String id = obj.getString("id");
LatLng pos = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(
pos).title(obj.getString("name")).
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
String local_id;
public GoogleMap.OnMarkerClickListener init(String id) {
local_id = id;
return this;
}
@Override
public boolean onMarkerClick(Marker marker) {
Intent i = new Intent(MapActivity.this, NextActivity.class);
i.putExtra("obj_id", local_id);
startActivity(i);
return false;
};
}.init(id));
} catch (JSONException e) {
//error handling
}
}
}