Change TextView in the xml file after two asynchronous requests - Java Android studio

50 Views Asked by At

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. Can anybody help me?

@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(".","");
            DataHandler dataHandler = new DataHandler();

            databaseReference = FirebaseDatabase.getInstance().getReference("chismesinfo");
            databaseReference.child(idLocation).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if (snapshot.exists()) {
                        descripcion = snapshot.child("descripcion").getValue(String.class);
                        user = snapshot.child("user").getValue(String.class);
                        firstOperation.complete(null);
                        usersReference = FirebaseDatabase.getInstance().getReference("users");
                        usersReference.child(user).addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                if (snapshot.exists()) {
                                    username = snapshot.child("name").getValue(String.class);
                                    if (snapshot.hasChild("profile")) {
                                        foto = snapshot.child("profile").getValue(String.class);
                                    } else {
                                        foto = null;
                                    }
                                    secondOperation.complete(null);
                                }
                            }
                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {
                                // Manejar errores si es necesario
                            }
                        });
                    }
                }
                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });

            if (foto != null) {
                byte[] imageAsByte = Base64.decode(foto.getBytes(), Base64.DEFAULT);
                Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsByte, 0, imageAsByte.length);
                imagen_mod.setImageBitmap(bitmap);
            }
            title_mod.setText(username);
            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.

1

There are 1 best solutions below

6
Gabe Sechan On

Your calls to setText are happening synchronously, but your network requests are asynchronous (as they need to be). That means the setText will happen before the network request. You need to call setText in the onDataChanged function, or in a call on the main thread made in onDataChanged if that happens off the main thread.