Firebase for a List

0

I need to get all the data from the firebase and create a list, so I can use it in a listadaper that I created. This is the Bank's structure in Firebase:

Asyoucansee,IwanttogetallthechildWallpapers(wallpaper1,wallpaper2,etc).

Ihavethefollowingcode:

DatabaseReferenceref=FirebaseDatabase.getInstance().getReference().child("Wallpapers").child("Wallpaper1");

And no value listener:

 ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<Wallpaper> list = new ArrayList<>();

            String Nome = (String) dataSnapshot.child("Nome").getValue();
            String Descricao = (String) dataSnapshot.child("Descricao").getValue();
            String URL = (String) dataSnapshot.child("URL").getValue();

            Wallpaper wallpaper = new Wallpaper(Nome, URL, Descricao);
            list.add(wallpaper);

            List_Adapter adapter = new List_Adapter(getApplicationContext(), list);
            lista_principal.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    };
    ref.addListenerForSingleValueEvent(eventListener);
}

But as it's kind of obvious, you're just getting the items from Wallpaper1; What I need is to get all Wallpapers items and add them to a List of this class:

public class Wallpaper {
private String Nome;
private String Url;
private String Descricao;

public Wallpaper(String nome, String url, String descricao) {
    Nome = nome;
    Url = url;
    Descricao = descricao;
}

So I can use it in the Listview adapter I created.

    
asked by anonymous 18.11.2017 / 21:52

1 answer

0

I was able to solve by doing the following. I removed the child from Wallpapers

 DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Wallpapers");

And I made a foreach on each Datasnapshot

for (DataSnapshot item : a) {
                String Nome = (String) item.child("Nome").getValue();
                String Descricao = (String) item.child("Descricao").getValue();
                String URL = (String) item.child("URL").getValue();
                Wallpaper wallpaper = new Wallpaper(Nome, URL, Descricao);

                list.add(wallpaper);
            }
    
19.11.2017 / 16:32