Home > Blockchain >  How to get data with ID from Firebase
How to get data with ID from Firebase

Time:08-05

Im currently working on Comic Reader app, and I want to code a Latest Updates page where all the new Chapters apear, I made 2 Firebase Database Childs one is for the Comics containing all the information and Chapters, and one called LatestUpdates only containing ID's of the coresponding comics.

How Can I use the ID's of the "LatestUpdates" Child to get the childs from "Comic"?

enter image description here

Currently my code gets the LatestUpdates childs with a for loop:

private void LoadLastUpdated() {
    latest.addListenerForSingleValueEvent(new ValueEventListener() {
        List<Comic> comic_load = new ArrayList<>();
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot snapshotcomic : snapshot.getChildren()) {
                Toast.makeText(getActivity(), snapshotcomic.toString(), Toast.LENGTH_SHORT).show();
            }
            comicListener.onComicLoadDoneListener(comic_load);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
}

Thanks!

CodePudding user response:

how Could I use the ID's to get the coresponding child in the other path.

To create a reference to the Comic from a snapshot of the latestUpdates child node you'd do:

DatabaseReference comicsRef = dbInstance.getReference("Comic");
DatabaseReference comicRef = comicsRef.child(snapshotcomic.getValue(String.class));

And then you can call addListenerForSingleValueEvent or getData on that comicRef.

  • Related