作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 Intents 在 Activity 之间传递数据。我已经可以发送数据了,但问题是数据只包含最后一项。
我不确定,但我认为问题出在基于以下引用的 for
语句:
这是我的代码。我希望有人能帮助解决我的问题。
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);
}
});
}
}
最佳答案
传递整个类对象比传递单个键值对更好。
PlaceItem placeItem = listData.get(finall).getId();
intent.putExtra("data", placeItem);
确保您的 PlaceItem 实现了Serializable
当你 getIntent 使用:
Intent intent = getIntent();
PlaceItem placeItem = (PlaceItem) intent.getSerializableExtra("data");
String id = placeItem.getId();
String cityName = placeItem.getCityName();
等等..
关于android - For 语句仅在使用 Intent 传递数据时返回最后一个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50012329/
我是一名优秀的程序员,十分优秀!