简体   繁体   English

在Firebase数据库中获取String值的正确方法是什么

[英]What is the correct way to get the String value in firebase database

Good day! 美好的一天!

I am having a problem in displaying the images. 我在显示图像时遇到问题。

Here is the code snippet: 这是代码片段:

final ArrayList<String> imgURLs = new ArrayList<>();

    String userUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    DatabaseReference databaseReference = firebaseDatabase.getReference(userUid).child("WARDROBE");
    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                imgURLs.add(snapshot.getValue().toString());
                Toast.makeText(Profile.this, (String)snapshot.getValue(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    imgURLs.add("https://assets.academy.com/mgen/58/20083458.jpg?is=500,500");
    imgURLs.add("https://assets.academy.com/mgen/61/20057861.jpg?is=500,500");
    imgURLs.add("https://assets.academy.com/mgen/65/20082765.jpg?is=500,500");
    imgURLs.add("https://ir0.mobify.com/project-burlington/1082/1922/https://cdn.burlingtoncoatfactory.com/FileLibrary/f4ee340d60f0421a8020b8ae05e6f36e/7854_SquareThumbnail.jpeg");
    imgURLs.add("https://qph.fs.quoracdn.net/main-qimg-5d9f95691aca6affb53dcae7fbeba61f-c");
    imgURLs.add("https://images.mattel.com/scene7//wcsstore/MattelCAS/05BUN14_Comfy_Space_PJs_and_Slippers_Girls_FMD28_FMD29_1?$ossmallindex$&storeId=10651&SKU=05BUN14");

My problem resides in the onDataChange. 我的问题在于onDataChange。 My target is to display the images I saved in the Firebase storage but first of all I created a Firebase Database with a child "Images" in it. 我的目标是显示保存在Firebase存储中的图像,但首先,我创建了一个带有子“图像”的Firebase数据库。 It must be a list of all https://. 它必须是所有https://的列表。

I created an example first. 我首先创建了一个示例。 I created a sample with child Images. 我创建了带有子图像的样本。 In it are the urls I added manually in my java code - which I got over the internet. 其中是我在Java代码中手动添加的网址-我是通过互联网获得的。 In Toast view the displayed string are the same as the urls I added manually in my java code. 在Toast视图中,显示的字符串与我在Java代码中手动添加的网址相同。 The problem is that it doesnt display the images from the URL in the firebase, it displays only the manually added java urls. 问题在于它不显示Firebase中URL的图像,它仅显示手动添加的Java url。

What makes this happen? 是什么使这种情况发生?

Okay, I have had a mistake. 好吧,我有一个错误。

The solution relies in onDataChange. 该解决方案依赖于onDataChange。 So, after the activity opened, the code is run from top to bottom and let's say the databaseReference was executed and onDataChange still takes time to take effect until there are changes. 因此,在活动打开之后,代码从上到下运行,并且假设执行了databaseReference,并且onDataChange仍然需要时间才能生效,直到发生更改为止。

so at the very bottom of my code there is setUpImageView() method and it was executed first before the onDataChange was finished. 因此,在我的代码的最底部,有setUpImageView()方法,它是在onDataChange完成之前首先执行的。 As a result, it doesnt display the image as wanted :D 结果,它不会按需要显示图像:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM