hrishi-1337
hrishi-1337

Reputation: 13

Unable to read data from Firebase database

CODE

final List<Shop> mShops = new ArrayList<>();
mRef.child("shops").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot noteSnapshot: dataSnapshot.getChildren()){
            Shop data = noteSnapshot.getValue(Shop.class);
            Log.e("log", data.getName()+" "+data.getUrl());
            Shops.add(data);
         }
     }

     @Override
     public void onCancelled(DatabaseError databaseError) {
         Log.d("LOG", databaseError.getMessage());
     }
});
for (Shop shop : mShops) {
    Log.e("mShops", shop.getName()+" "+shop.getUrl());
}

LOG

E/log: Levi's https://firebasestorage.googleapis.com/v0/b/fir-87605.appspot.com/o/storefront27.jpg?alt=media&token=4ea27811-4e06-4bf5-8b96-44a583040088
E/log: Dunkin Donuts https://firebasestorage.googleapis.com/v0/b/fir-87605.appspot.com/o/dunkin.jpg?alt=media&token=f63c6d5f-b074-47ae-a8ca-6f7974ddbd47
E/log: Pizza Parlor https://firebasestorage.googleapis.com/v0/b/fir-87605.appspot.com/o/download.jpg?alt=media&token=1eb9a9bb-495d-416b-9a84-3699214149b9
E/log: Town Bakers https://firebasestorage.googleapis.com/v0/b/fir-87605.appspot.com/o/download%20(1).jpg?alt=media&token=01b16918-272f-4a9f-9607-61f559d30360

The data is being read from the database within onDataChange but i am unable to store and read it from the mShops list.I would appreciate any help in find out the reason why this data isn't being stored in the list.

Upvotes: 1

Views: 431

Answers (2)

Niraj Niroula
Niraj Niroula

Reputation: 2424

Edited after Alex's suggestions

What you can do here is pass every data from onDataChange() to another method and access it from there or if you're just planning to print the data on log try shifting this line(without final)

List<Shop> mShops = new ArrayList<>();    

inside this

mRef.child("shops").addValueEventListener(new ValueEventListener() {}

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 1

To solve this, simply move the declaration of your mShops list inside the onDataChange() like this:

mRef.child("shops").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<Shop> mShops = new ArrayList<>();

        for (DataSnapshot noteSnapshot: dataSnapshot.getChildren()){
            Shop data = noteSnapshot.getValue(Shop.class);
            Log.e("log", data.getName()+" "+data.getUrl());
            mShops.add(data);
         }

         for (Shop shop : mShops) {
             Log.e("mShops", shop.getName()+" "+shop.getUrl());
         }
     }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.d("LOG", databaseError.getMessage());
    }
});

Note, that with this code, your ArrayList does not need to be final. Remember, onDataChange() method has an asynchronous behavior which means that is called even before you are trying to add those objects of class Shop to the list.

Upvotes: 2

Related Questions