Tell me how to solve this problem.

Please tell me how to display text without a final String in InfoWindow, otherwise all the markers will be with the same text. I need to display in the InfoWindow order 3-4 different lines. Below is a snippet of code from the screen.

 public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) { if(dataSnapshot.child("latitude").getValue() != null && dataSnapshot.child("longitude").getValue() != null) { double latt = dataSnapshot.child("latitude").getValue(Double.class); double longg = dataSnapshot.child("longitude").getValue(Double.class); LatLng newLocation = new LatLng(latt, longg); String sname = (String) dataSnapshot.child("sname").getValue(); String text = (String) dataSnapshot.child("text").getValue(); String Number = (String) dataSnapshot.child("number").getValue(); String image = dataSnapshot.child("image").getValue(String.class); if (image.equals("akym")) { mMap.addMarker(new MarkerOptions() .position(newLocation) .title(sname) .snippet("Номер:"+Number+"\n"+"Описание:"+text) .icon(BitmapDescriptorFactory.fromResource(R.mipmap.akym)) ); mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { @Override public View getInfoWindow(Marker arg0) { return null; } @Override public View getInfoContents(Marker marker) { View myContentView = getLayoutInflater().inflate( R.layout.custommarker, null); TextView tvTitle = ((TextView) myContentView .findViewById(R.id.title)); tvTitle.setText(Number); TextView tvSnippet = ((TextView) myContentView .findViewById(R.id.snippet)); tvSnippet.setText(marker.getSnippet()); return myContentView; } }); 

PS I know that the code is terrible, but I'm learning)

  • You need to install InfoWindowAdapter once, and transfer the data for display through setTag / getTag from Marker ( example ). - zRrr 4:39 pm
  • @zRrr, unfortunately I didn’t quite understand how to use it ... - Nasdomlan Urban3p 5:47 pm

1 answer 1

To access the variable in the inner class of the method, there are several options:

  1. You can either make the variable global (set in the class body) and only change / set the value in the method.

  2. You can make a final array variable with a single element inside the method: final String[] number = new String[1] and work with it: number[0] = "Текст" ; tvTitle.setText(number[0]) , etc.

  • .snippet("Имя:"+sname+"\n"+"Номер:"+Number+"\n"+"Описание:"+text) public View getInfoContents(Marker marker) { View myContentView = getLayoutInflater().inflate( R.layout.custommarker, null); TextView tvSnippet = ((TextView) myContentView .findViewById(R.id.snippet)); tvSnippet.setText(marker.getSnippet()); return myContentView; This is how it got to be - Nasdomlan Urban3p February pm
  • I did what I wanted in principle) But thanks for the answer) - Nasdomlan Urban3p February 5:09