简体   繁体   中英

How to Access Many Nested Child Firebase Realtime database into RecyclerView?

Can anyone here help me?

how to display the "Episode" and "Pemeran" sections in the recyclerView on the second display in the following image.

I use the same method as "liveTV" but it doesn't work / appears anything, because of the many children in the Episode and Pemeran tables?

please help, thank you

Firebase Realtime-database picture]

火力


Application Concept

概念

dbDrakorAdapter.java - Models

public class dbDrakorAdapter {
private String LinkMovie;
private String List;
private String FotoPemain;
private String NamaPemain;


public dbDrakorAdapter (String linkMovie, String list, String fotoPemain, String namaPemain) {
    LinkMovie = linkMovie;
    List = list;
    FotoPemain = fotoPemain;
    NamaPemain = namaPemain;


}
public class Episode {
    List<EpisodeDetails> episodeDetails;
}

public class EpisodeDetails {
   // public Long chapterId;
    public String LinkMovie;
    public String List;
}
public class Pemeran {
    List<PemeranDetails> pemeranDetails;
}

public class PemeranDetails {
    // public Long chapterId;
    public String LinkMovie;
    public String List;
}
@PropertyName("LinkMovie")
public String getLinkMovie() { return LinkMovie; }
@PropertyName("List")
public String getList() { return List; }
@PropertyName("FotoPemain")
public String getFotoPemain() { return FotoPemain; }
@PropertyName("NamaPemain")
public String getNamaPemain() { return NamaPemain; }

@Exclude
public Map<String, Object> toMap() {
    HashMap<String, Object> result = new HashMap<>();
    result.put("List", List);
    result.put("LinkMovie", LinkMovie);
    result.put("FotoPemain", FotoPemain);
    result.put("NamaPemain", NamaPemain);
    return result;
}
}

DrakorActivity.Java

public class DrakorActivity extends AppCompatActivity {

private RecyclerView mEpisodeRV,mPemeranRV;
private FirebaseRecyclerAdapter<dbDrakorAdapter, DrakorActivity.ListEpisode> mListEpisodeAdapter;
private FirebaseRecyclerAdapter<dbDrakorAdapter, DrakorActivity.ListPemeran> mListPemeranAdapter;

FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drakor);

    DatabaseReference database = FirebaseDatabase.getInstance().getReference("SerialDrakor").child("Episode");
    Query personsQuery = database.orderByKey();
    mEpisodeRV = (RecyclerView) findViewById(R.id.cRecylerDrakorEpisode);
    mEpisodeRV.setHasFixedSize(true);
    mEpisodeRV.setLayoutManager(new LinearLayoutManager(this));
    FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<dbDrakorAdapter>().setQuery(personsQuery, dbDrakorAdapter.class).build();

    mListEpisodeAdapter = new FirebaseRecyclerAdapter<dbDrakorAdapter, ListEpisode>(personsOptions) {

        @Override
        protected void onBindViewHolder(DrakorActivity.ListEpisode holder, int position, final dbDrakorAdapter model ) {
            holder.setList(model.getList());

            holder.mView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    Toast.makeText(DrakorActivity.this,"Ini Hanya Iklan , Tidak Dapat Digunakan",Toast.LENGTH_LONG).show();
                }
            });
        }


        @Override
        public DrakorActivity.ListEpisode onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_drakor_episode, parent, false);

            return new DrakorActivity.ListEpisode(view);


        }



    };
    LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
    mEpisodeRV.setLayoutManager(layoutManager);
    mEpisodeRV.setAdapter(mListEpisodeAdapter);

}
@Override
public void onStart() {
    super.onStart();
    mListEpisodeAdapter.startListening();
  //  mListPemeranAdapter.startListening();

}

@Override
public void onStop() {
    super.onStop();
    mListEpisodeAdapter.stopListening();
  //  mListPemeranAdapter.stopListening();

}

//Episode
public static class ListEpisode extends RecyclerView.ViewHolder{
    View mView;
    public ListEpisode(View itemView){
        super(itemView);
        mView = itemView;
    }
    public void setList (String list){
        TextView post_title = (TextView)mView.findViewById(R.id.TxtlistEpisode);
        post_title.setText(list);
    }
}

activity_drakor.xml

<LinearLayout
    android:id="@+id/drakorLyEpisode"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_below="@id/drakorThumbnail"
    android:background="@color/colorAccent"

    android:orientation="horizontal">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/cRecylerDrakorEpisode"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>
</LinearLayout>

item_drakor_episode.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="30dp"
android:orientation="horizontal"

xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardBackgroundColor="#090000"
    app:cardCornerRadius="15dp">

    <TextView
        android:id="@+id/TxtlistEpisode"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#ffffff"
        android:textSize="20dp"
        android:text="1"
        android:textAlignment="center"
        android:textStyle="bold" />
</android.support.v7.widget.CardView>

Your FirebaseRecyclerAdapters are using the same POJO but they are going to display different information. For example:

FirebaseRecyclerAdapter<EpisodePojo, DrakorActivity.ListEpisode> listEpisodesAdapter
FirebaseRecyclerAdapter<PemeranPojo, DrakorActivity.ListPemeran> listPemeranAdapter

Your code is not showing any information probably because your DatabaseReference is trying to access "Episode as a child of SerialDrakor" but "SerialDrakor" is a list of objects, therefore, your code will not be able to parse a list of "SerialDrakor" child into "Episode".

You have to retrieve "SerialDrakor" as a list and use the child you need from this list to retrieve the "Episode" and "Pemeran" inside of it.

Here you can see some examples, maybe would be a good idea to use a custom parser.

https://github.com/firebase/FirebaseUI-Android/tree/master/database#using-the-firebaserecycleradapter

Update: What I would do is create a class parsing a "SerialDrakor" item.

class SerialDrakor {
   List<Episode> episodeList;
   List<Pemeran> pemeranList;

   .....
}

Then use a query to retrieve "SerialDrakor"

DatabaseReference database = FirebaseDatabase.getInstance().getReference();
Query serialDrakorQuery = database.child("SerialDrakor").orderByKey();
FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<SerialDrakor>().setQuery(serialDrakorQuery, new SnapshotParser<SerialDrakor>() {
@Override
public SerialDrakor parseSnapshot(@NonNull DataSnapshot snapshot) {
   // Here fetch the data you want and pass it to your adapters
}

I hope it helps.

On deep seeing your firebase structure it is difficult to query as you said.So you can change your firebase structure as below

Episode
     |
     |__Unique_Key
           |
           |__LinkMovie:"asasa.com
           |__List:"1"
           |__Epi:"1"
  Pemeran
      |
      |__UniqueKey
             |__fotoPermain:"...."
             |__NamaPemain:"Ronny"
             |__permeranId:"1"

Here,SubNode 1 in your Episode and pemeran Node in your firebase structure is replace as the Child Node of Unique Key. You can generate Unique Key by push()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM