我试图自定义我的Infowindow,但我不能将TextViews与字符串绑定。 InfoWindow有时会显示原始xml,有时只是不起作用。我觉得自己有点失落。
加载功能似乎工作正常。在我看来,问题始于mMap.addMarker函数。
private void placeAllMarkers() {
mContext = getActivity();
mPrefManager = new PrefManager(mContext);
List <MapMarker> allMapMarker;
allMapMarker = mPrefManager.loadAllMapMarker();
if (allMapMarker.size() > 0) {
for (int counter = 0; counter < allMapMarker.size(); counter++) {
price = allMapMarker.get(counter).getSum();
shop = allMapMarker.get(counter).getShop();
date = allMapMarker.get(counter).getDate();
Float GPS_X = allMapMarker.get(counter).getGPS_X();
Float GPS_Y = allMapMarker.get(counter).getGPS_Y();
LatLng Position = new LatLng(GPS_X, GPS_Y);
mMap.addMarker(new MarkerOptions()
.position(Position)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
InfoView = getActivity().getLayoutInflater().inflate(R.layout.custom_infowindow, null);
setShop = (TextView) InfoView.findViewById(R.id.setShop);
setSum = (TextView) InfoView.findViewById(R.id.setSum);
setDate = (TextView) InfoView.findViewById(R.id.setDate);
setShop.setText(shop);
setSum.setText(price);
setDate.setText(date);
return InfoView;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
});
}
}
}
感谢您的时间。
编辑
这是字符串:
致命的例外:主要 过程:hu.mosomaci.flagsproject,PID:4983 android.content.res.Resources $ NotFoundException:字符串资源ID#0xd98 在android.content.res.Resources.getText(Resources.java:252) 在android.content.res.MiuiResources.getText(MiuiResources.java:108) 在android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52) 在android.widget.TextView.setText(TextView.java:3895) at hu.mosomaci.flagsproject.fragments.MapForFlags $ 1.getInfoContents(MapForFlags.java:136) 在com.google.android.gms.maps.GoogleMap $ 7.zzi(未知来源) 在com.google.android.gms.maps.internal.zzd $ zza.onTransact(未知来源) 在android.os.Binder.transact(Binder.java:361)
答案 0 :(得分:0)
首先设置下面的标记。设置价格和日期的代码段,并在它们之间添加%符号。
mMap.addMarker(new MarkerOptions()
.position(Position)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
.title(shop)
.snippet(price + "%"+date));
尝试在getInfoContents(Marker marker)
方法中设置文本和其他工作(因为getInfoContents(Marker marker)
用于定义内容。)而不是getInfoWindow(Marker marker)
setInfoWindowAdapter
。
将文本设置为TextView时,会拆分代码段并按索引获取价格和日期
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
InfoView = getActivity().getLayoutInflater().inflate(R.layout.custom_infowindow, null);
TextView setShop = (TextView) InfoView.findViewById(R.id.setShop);
TextView setSum = (TextView) InfoView.findViewById(R.id.setSum);
TextView setDate = (TextView) InfoView.findViewById(R.id.setDate);
String snippet = marker.getSnippet();
String[] splited_text = snippet.split("%");
String price = splited_text[0];
String date = splited_text[1];
setShop.setText(marker.getTitle());
setSum.setText(price);
setDate.setText(date);
return InfoView;
}
});