I'm trying to get info from firestore on user click on a marker and show it in the custom info window, the problem is I have to click multiple times before I get the updated values. What would be the best way to do this?
this is the code I have:
public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
private final View mWindow;
private Context mContext;
private FirebaseFirestore db;
private String selectedStation;
private long safeVotes;
private long unsafeVotes;
public CustomInfoWindowAdapter(Context mContext) {
db = FirebaseFirestore.getInstance();
this.mContext = mContext;
mWindow = LayoutInflater.from(mContext).inflate(R.layout.custom_info_window, null);
}
private void renderWindowText(final Marker marker, final View view) {
db.collection("game1").document("position").collection("collection").whereEqualTo("location", marker.getPosition())
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (QueryDocumentSnapshot document : task.getResult()) {
selectedStation = document.getId();
DocumentReference currentStationRef = db.collection("game1").document("position").collection("1").document(selectedStation);
currentStationRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
long safeVotes = 0;
long unsafeVotes = 0;
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
// Toast.makeText(MapActivity.this, "" + document.getData(), Toast.LENGTH_SHORT).show();
Map<String, Object> stationHashmap = document.getData();
safeVotes = (long) stationHashmap.get("safe votes");
CustomInfoWindowAdapter.this.safeVotes = safeVotes;
unsafeVotes = (long) stationHashmap.get("unsafe votes");
CustomInfoWindowAdapter.this.unsafeVotes = unsafeVotes;
// Toast.makeText(MapActivity.this, "safe: " + safeVotes + "; unsafe votes: " + unsafeVotes, Toast.LENGTH_SHORT).show();
//putting info in the custom info window
String title = marker.getTitle();
TextView titleTextView = view.findViewById(R.id.station_name);
titleTextView.setText(title);
TextView safeVotesTextView = view.findViewById(R.id.safe_votes);
safeVotesTextView.setText("SafeVotes: " + safeVotes);
TextView unsafeVotesTextView = view.findViewById(R.id.unsafe_votes);
unsafeVotesTextView.setText("Unsafe Votes: " + unsafeVotes);
String snippet = marker.getSnippet();
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
}
// Toast.makeText(MapActivity.this, "SELECTED: " + selectedStation, Toast.LENGTH_SHORT).show();
// selectedStationTextView.setText(selectedStation);
}
});
}
@Override
public View getInfoWindow(Marker marker) {
renderWindowText(marker, mWindow);
return mWindow;//before returning this I want renderWindowText(marker, mWindow); to complete fetching data
}
@Override
public View getInfoContents(Marker marker) {
renderWindowText(marker, mWindow);
return mWindow;
}
}
I think it's possible to add a delay before returning the mWindow, but it doesn't seem like the best option. Can I return the view from inside the OnCompleteListener?