user15994341
user15994341

Reputation:

How to get only specific read data from firebase for a particular child node

Heyy Developers, I'm creating a Tv series app in which I store Season no, episodes no, etc. For storing data I'm using Firebase realtime database but I want to retrive Season no only in a recyclerview when I click on a specific series but database returns every season no in my recycelrview for every series, for ex. In my database I store two series Money heist and Moon knight and each have 2 and 3 seasons respectively, but when I click on Money heist it returns me combine of both series that is 5 instead it should return 2 only and same thing happens for moon knight. I'm using recyclerview to show series list by reading data from realtime database.

  "MONEY HEIST ": {    <------- I'm creating this series name node 
                                                programatically

    "Season": {
      "Season 1": {
        "Episodes": {
          "episode no": 1
        },
        "season_no": "Season 1"
      },
      "Season 2": {
        "Episodes": {
          "Episode 2": "jsdfjfjskkshfksdfksdfsdsfsdjfljforhfvbincieefnlsclsladlasdlefiowfjkdnckcdbebckwcnkwhefksdnfksdnfksdfnksnfksdnskdfnksdnfksndfksndfksndfksndfdfsdfndsfns,dnf"
        },
        "season_no": "Season 2"
      }
    }
  },
  "MOON KNIGHT ": {
    "Season": {
      "Season 1": {
        "Episodes": {
          "Episode 1": ""
        },
        "season_no": "Season 1"
      }
    }
  }
}

I want to show only season no of specific series name means if I click on Moon knight it should show only its seasons and not of others and same for others.

So far i tried this method to retrive data from firebase,

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {



                for (DataSnapshot ds : snapshot.getChildren())
                {

                    for (DataSnapshot pc : ds.getChildren())
                    {


                            for (DataSnapshot bi : pc.getChildren()) {
                                season_number number = bi.getValue(season_number.class);
                                details.add(number);


                        }
                    }






                }
                series = new Adapter_ofreal_season(details);
                recyclerView.setAdapter(series);
            }

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

            }
        });

And im creating Series name node programatically..

Upvotes: 0

Views: 151

Answers (1)

Stefan de Kraker
Stefan de Kraker

Reputation: 363

If you make your Firebase look like this:

FireBase structure

so every serie will have its own document within the collection Series And every serie has a collection of Seasons, which has Episodes and so on.

Then when you just want to have all data of "MONEY HEIST"

you can do:

docRef = db.collection("series").document("MONEY HEIST");

docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            // Document found
            DocumentSnapshot document = task.getResult();
            Log.d(TAG, "document data: " + document.getData());
        } else {
            Log.d(TAG, "failed: ", task.getException());
        }
    }
});

source
edit:
Also, you could map the results of your call to an Object. In this way you can easily use the data in your program. This is done with the toObject() call. Keep in mind that then your Firebase document and Object have the same naming and no-argument constructors (You can read more about this here).

Upvotes: 0

Related Questions