【发布时间】:2018-10-05 08:14:46
【问题描述】:
我想使用 Intent 在活动之间传递数据。我已经可以发送数据,但问题是数据只包含最后一项。
我不确定,但我认为问题出在 for 语句基于以下参考:
- Parsing JSON to custom ArrayList, only returning last item?
- Listview only displaying last item of arraylist
这是我的代码。我希望有人可以帮助解决我的问题。
private void getAllDataLocation() {
loading = ProgressDialog.show(this, null, "Menampilkan Semua Tempat...", true, false);
mApiService.getAllPartner().enqueue(new Callback<ResponsePartner>() {
@Override
public void onResponse(Call<ResponsePartner> call, Response<ResponsePartner> response) {
if (response.isSuccessful()) {
loading.dismiss();
final List<PlaceItem> placeItems = response.body().getAllPlace();
initMarker(placeItems);
} else {
loading.dismiss();
Toast.makeText(mContext, "Gagal Mengambil Data Semua Tempat", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponsePartner> call, Throwable t) {
loading.dismiss();
Toast.makeText(mContext, "Koneksi Internet Bermasalah", Toast.LENGTH_SHORT).show();
}
});
}
和
private void initMarker(final List<PlaceItem> listData) {
//for each semua data dan tampilkan markernya
for (int i = 0; i < listData.size(); i++) {
final int finall = i;
//set latlng nya
LatLng marker_location = new LatLng(Double.parseDouble(listData.get(i).getLatitude()), Double.parseDouble(listData.get(i).getLongitude()));
//tambah markernya
marker_bg.setColorFilter(getResources().getColor(R.color.marker_primary));
MarkerOptions markerOptions = new MarkerOptions().title(listData.get(i).getName()).position(marker_location);
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(Tools.createBitmapFromView(ActivityMaps.this, marker_view)));
mMap.addMarker(markerOptions);
//start for marker specific zoom ex: for specific city you want to zoom
LatLng marker_zoom_specific = new LatLng(Constant.city_lat, Constant.city_lng);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(marker_zoom_specific, 12));
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
String id = listData.get(finall).getId();
String cityName = listData.get(finall).getCityName();
String description = listData.get(finall).getDescription();
String address = listData.get(finall).getAddress();
String phone = listData.get(finall).getPhone();
String website = listData.get(finall).getWebsite();
String logo = listData.get(finall).getLogo();
String toolbarTitle = listData.get(finall).getName();
String latitude = listData.get(finall).getLatitude();
String longitude = listData.get(finall).getLongitude();
Intent toDetail = new Intent(ActivityMaps.this, ActivityPlaceDetail.class);
toDetail.putExtra(Constant.KEY_ID_PARTNER, id);
toDetail.putExtra(Constant.KEY_NAME, toolbarTitle);
toDetail.putExtra(Constant.KEY_CITY_NAME, cityName);
toDetail.putExtra(Constant.KEY_DESCRIPTION, description);
toDetail.putExtra(Constant.KEY_ADDRESS, address);
toDetail.putExtra(Constant.KEY_PHONE, phone);
toDetail.putExtra(Constant.KEY_WEBSITE, website);
toDetail.putExtra(Constant.KEY_LOGO, logo);
toDetail.putExtra(Constant.KEY_LATITUDE, latitude);
toDetail.putExtra(Constant.KEY_LONGITUDE, longitude);
startActivity(toDetail);
}
});
}
}
【问题讨论】:
标签: android arrays google-maps for-loop android-intent