请求 Firebase 后更改 TextView 中的值 - Java Android studio

问题描述 投票:0回答:1

我向 Firebase 数据库发出两个请求,然后将数据加载到 XML 中。但它不会加载它们,只是保留默认值,我怀疑这是因为我没有在我正在工作的片段(GoogleMap.InfoWindowAdapter)中正确处理异步请求。有人可以帮助我吗?

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;
            }
}
}

我原本希望数据直接加载到界面,但我只是默认获取文本。但是,我第二次触发该事件时,它确实加载了数据。

java android firebase android-asynctask
1个回答
0
投票

数据是从 Firebase(以及大多数现代云 API)异步加载的,因为可能需要一些时间才能可用。加载数据时,您的主要代码将继续 - 以确保用户可以继续使用该应用程序。然后,当加载数据时,您的

onComplete
将使用该数据进行调用。

这在实践中意味着您的

Base64.decode(foto.getBytes()...
foto = snapshot1.child("profile").getValue(String.class);
运行之前被调用。如果您添加一些日志记录或设置断点并在调试器中运行,您可以很容易地看到这一点。

为了避免此问题,任何需要数据库数据的代码都必须位于

onComplete
内部,从那里调用,或者以其他方式同步。有关如何执行此操作的示例(以及更多信息),请参阅:

© www.soinside.com 2019 - 2024. All rights reserved.