简体   繁体   English

从 Firebase 实时数据库获取 url 值

[英]get the url values from Firebase Realtime database

在此处输入图片说明 I have the above format in firebase realtime database I need to get data in arraylist我在firebase实时数据库中有上述格式我需要在arraylist中获取数据

public class EmojiView extends AppCompatActivity {


FirebaseUser firebaseUser;
DatabaseReference reference;

ArrayList<String> images = new ArrayList<>();

private void getImagesfromDB() {
    firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    reference = FirebaseDatabase.getInstance().getReference("stickers");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                String data = snapshot.getValue(String.class);
                images.add(data);
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_emoji_view);
    getImagesfromDB();

}
}

I get the urls in data variable in OnDataChangemethod, but i am not able to assign those values to images arraylist我在 OnDataChangemethod 中获取数据变量中的 url,但我无法将这些值分配给图像数组列表

Try this:尝试这个:

Change your reference to this:更改您对此的引用:

reference = FirebaseDatabase.getInstance().getReference("stickers").getChildren();

And then loop through the snapshots (which should be the children of "stickers") this way:然后以这种方式遍历快照(应该是“贴纸”的子代):

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        
        for (DataSnapshot item : snapshot){
            stickers.add(item.getValue(String.class));
        }    

    }

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

    }
});

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

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