I make two requests to the Firebase database, and then load the data into the XML. But it doesn't load them and just leaves the default, I suspect it's because I'm not handling asynchronous requests properly in the fragment that Im working (GoogleMap.InfoWindowAdapter). Can anybody help me?
public class MyInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
Context context;
static String user, descripcion, username, foto;
DatabaseReference databaseReference, usersReference;
TextView title_mod,snipp_mod;
ImageView imagen_mod;
public MyInfoWindowAdapter(Context context) {
this.context = context;
}
@Override
public View getInfoWindow(@NonNull Marker marker) {
if (!marker.getTitle().toString().equals("Este es mi perfil")) {
View infoView = LayoutInflater.from(context).inflate(R.layout.custom_info, null);
title_mod = infoView.findViewById(R.id.title_mod);
snipp_mod = infoView.findViewById(R.id.snipp_mod);
imagen_mod = infoView.findViewById(R.id.imagen_mod);
LatLng posicionMarker = marker.getPosition();
String idLocation = String.valueOf(posicionMarker.latitude).replace(".", "") + "_" + String.valueOf(posicionMarker.longitude).replace(".", "");
databaseReference = FirebaseDatabase.getInstance().getReference("chismesinfo");
databaseReference.child(idLocation).get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
DataSnapshot snapshot = task.getResult();
descripcion = snapshot.child("descripcion").getValue(String.class);
user = snapshot.child("user").getValue(String.class);
usersReference = FirebaseDatabase.getInstance().getReference("users");
usersReference.child(user).get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
DataSnapshot snapshot1 = task.getResult();
username = snapshot1.child("name").getValue(String.class);
if (snapshot1.hasChild("profile")) {
foto = snapshot1.child("profile").getValue(String.class);
} else {
foto = null;
}
}
}
});
}
}
});
byte[] imageAsByte = Base64.decode(foto.getBytes(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsByte, 0, imageAsByte.length);
imagen_mod.setImageBitmap(bitmap);
snipp_mod.setText(descripcion);
return infoView;
}
}
}
I was expecting the data to be loaded directly to the interface, but instead I just get the text by default. However, the second time I trigger the event, it does load the data.