在我的应用中,我尝试从Firebase检索一些数据,将它们显示为自定义信息窗的两个texviews。在地图上放置每个标记之后,在onDataChange方法上我可以正确获取logcat中的所有名称,但是我无法在textView中设置它们的名称信息和cityInfo。 有任何想法吗?我的错误在哪里?提前谢谢。
这是我的数据库的屏幕截图:firebase screenshot
这是我的MapsActivity.java
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
DatabaseReference ref1 = FirebaseDatabase.getInstance().getReference();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
//add markers method
ref1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnap : dataSnapshot.getChildren()) {
String latitude = childSnap.child("coords").child("latitude").getValue().toString();
String longitude = childSnap.child("coords").child("longitude").getValue().toString();
double lat = Double.parseDouble(latitude);
double lon = Double.parseDouble(longitude);
LatLng cod = new LatLng(lat, lon);
mMap.addMarker(new MarkerOptions().position(cod));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//add infowindows method
if (mMap != null) {
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View view = getLayoutInflater().inflate(R.layout.user_infowindow, null);
final TextView nameInfo = (TextView) view.findViewById(R.id.nameinfowindow);
final TextView cityInfo = (TextView) view.findViewById(R.id.cityinfowindow);
ref1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
//List<String> lst = new ArrayList<String>();
Log.i("Count ", "" + dataSnapshot.getChildrenCount());
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
String nome = postSnapshot.child("nome").getValue().toString();
nameInfo.setText(nome);
Log.i("nomi", nome);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return view;
}
});
}
}
}
这是我的infowindow布局:user_infowindow
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/nameinfowindow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nome.."
android:layout_gravity="top|start" />
<TextView
android:id="@+id/cityinfowindow"
android:layout_width="wrap_content"
android:text="city.."
android:layout_height="wrap_content" />
</LinearLayout>