简体   繁体   中英

get the url values from Firebase Realtime database

在此处输入图片说明 I have the above format in firebase realtime database I need to get data in 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

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) {

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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