DavideN
DavideN

Reputation: 253

FirebaseRecyclerAdapter with nested elements

Problem

enter image description here

I would like to show a list of elements but those elements are nested inside groups ("playlist" in the example). The data of the elements to show are instead in another node ("movies" in the example).

I tried with 2 different approaches but nothging works:

(1) With FirebaseRecyclerAdapter

In FirebaseRecyclerAdapter I can pass only a databasereference and in that case would be getReference(MoviePlayLists).child(User-Id-1) and it doesn't work.

I tried to create my custom model class on those nodes and override in the adapter parseSnapshot() but I cannot populate the View correctly in populateView() because it is called on "MoviePlayLists" nodes and not sub-nodes.

(2) With FirebaseIndexRecyclerAdapter

Because the reference for keys is again nested I'm not able to use it.


Do you have any idea on the correct approach to do it with this DB structure? Is it possible?

Upvotes: 1

Views: 700

Answers (1)

Majeed Khan
Majeed Khan

Reputation: 505

I am not sure about the correct way of doing it but here is how I would implement it

Query keyref = FirebaseDatabase.getInstance().getReference().child("User-Id");
Query dref = FirebaseDatabase.getInstance().getReference().child("User-Id");

now create a class extending FirebaseIndexRecyclerAdapter and attach it to the recyclerView.

now int the populateViewHolder() method fetch the movie-id details

populateViewHolder(final VIEW_HOLDER vh, final String movie_id, final int position){

 FirebaseDatabase.getInstance().getReference().child(movie_id).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Movie m = dataSnapshot.getValue(MODEL_CLASS_FOR_MOVIE_ID);

           // update you movie details here

        }
    });
}

If anyone has a better solution please comment!

Upvotes: 1

Related Questions