0

So I'm making a wallpaper app that has various categories but I want to display all images from all nodes in Firebase in one fragment called Random, I also want the Images to shuffle images from each parent node

The following is my Firebase structure : These are all the nodes

There are also child nodes in them : There are also three child nodes in each of them containing links to images

The following is my java code from my Random fragment :

    private void getWallpapers() {
    progressBar.setVisibility(View.VISIBLE);
    myRef = database.getReference().child("Wallpaper").child("Random");
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            collectionsArray.clear();

            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                wallpaper z = postSnapshot.getValue(wallpaper.class);
                collectionsArray.add(z);
            }
            Collections.reverse(collectionsArray);
            progressBar.setVisibility(View.GONE);
            mAdapter.notifyDataSetChanged();
        }


        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            System.out.println("Error Reading from DB");
        }
    });
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
agega
  • 157
  • 3
  • 13
  • What is the problem with the code you currently have? Could you provide the result or the error you received? – Adrian Russo Jan 02 '21 at 22:57
  • There is no error I'm receiving @AdrianRusso , The problem I'm facing is I can display all the Images in one fragment from my firebase in one fragment. – agega Jan 03 '21 at 03:20

1 Answers1

1

There is no built-in query that can get elements across different nodes in the database. Seeing that you have different structures for each category, the best option I can think of is to duplicate the data. This practice is called denormalization and is a common practice when it comes to Firebase. For a better understanding, I recommend you see this video, Denormalization is normal with the Firebase Database.

So you should create another node that will hold all images you have in the database, no matter from which category belongs. To select a random wallpaper, please check my answer from the following post:

Also remember, when you are duplicating data, there is one thing that you need to keep in mind. In the same way, you are adding data, you need to maintain it. In other words, if you want to update/delete a wallpaper, you need to do it in every place that it exists.

If you might also be interested in:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193