0

Multiple viewType using FirebaseRecyclerAdapter. I studied about RecyclerView that have multiple viewType. And I'm going to apply it in FirebaseRecyclerApdater. I want to show the data in Firebase Realtime Database. But anything shows up in-app. Here my code.

public class HolderHomeAdapter extends RecyclerView.Adapter<HolderHomeAdapter.HomeSectionHolder> {

private ArrayList<SectionType> sectionList;
private Context context;

FirebaseRecyclerOptions<ContentModel> options;
public static FirebaseRecyclerAdapter recyclerAdapter;

public HolderHomeAdapter(Context context, ArrayList<SectionType> sectionList) {
    this.context = context;
    this.sectionList = sectionList;
}

@NonNull
@Override
public HomeSectionHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.holder_section_home, viewGroup, false);
    return new HomeSectionHolder(view);
}

@Override
public void onBindViewHolder(@NonNull HomeSectionHolder holder, int i) {
    SectionType tempSection = sectionList.get(i);

    int type = tempSection.getSectionType();
    String title = tempSection.getTitle();

    holder.holderText.setText(title);
    holder.holderRecycler.setHasFixedSize(true);
    holder.holderRecycler.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

    if (type == TYPE_01) {
        Query type01Query = FirebaseDatabase.getInstance().getReference().child("TYPE_01");
        options = new FirebaseRecyclerOptions.Builder<ContentModel>().setQuery(type01Query, ContentModel.class).build();
        recyclerAdapter = new Type01Adapter(options);
        holder.holderRecycler.setAdapter(recyclerAdapter);
    } else if (type == TYPE_02) {
        Query type02Query = FirebaseDatabase.getInstance().getReference().child("TYPE_02");
        options = new FirebaseRecyclerOptions.Builder<ContentModel>().setQuery(type02Query, ContentModel.class).build();
        recyclerAdapter = new Type02Adapter(options);
        holder.holderRecycler.setAdapter(recyclerAdapter);
    } else if (type == TYPE_03) {
        Query type03Query = FirebaseDatabase.getInstance().getReference().child("TYPE_03");
        options = new FirebaseRecyclerOptions.Builder<ContentModel>().setQuery(type03Query, ContentModel.class).build();
        recyclerAdapter = new Type03Adapter(options);
        holder.holderRecycler.setAdapter(recyclerAdapter);
    } else {
        Query type04Query = FirebaseDatabase.getInstance().getReference().child("TYPE_04");
        options = new FirebaseRecyclerOptions.Builder<ContentModel>().setQuery(type04Query, ContentModel.class).build();
        recyclerAdapter = new Type04Adapter(options);
        holder.holderRecycler.setAdapter(recyclerAdapter);
    }

}

@Override
public int getItemCount() {
    return sectionList.size();
}
}

Above HolderHomeAdapter class called in MainActivity. I want to know when I have to call the method startListening() and stopListening().

H.JiMan
  • 269
  • 1
  • 2
  • 10
  • What happened when you followed the suggestion in [this answer](https://stackoverflow.com/a/55025886) that you were linked to from [your previous question](https://stackoverflow.com/q/57695612)? – Mike M. Aug 29 '19 at 03:03
  • There is no @Override method onStart() and onStop(). Because of recyclerAdapter called in Adapter class. – H.JiMan Aug 29 '19 at 03:13
  • Ah, OK, I see the confusion. Those methods are in the `Activity`. If you need them in your `HolderHomeAdapter` to start and stop those internal `Adapter`s, you could simply add methods to `HolderHomeAdapter` that you can call from the `Activity`'s methods, and will call `startListening()` and `stopListening()` on the internal ones. For example, `public void start() { if (recyclerAdapter != null) recyclerAdapter.startListening(); }`. – Mike M. Aug 29 '19 at 03:15
  • Thank you! I added the method in the `if (type == TYPE_01) { ... start() ... }`. But... when I run this app, it doesn't show anything. However, I write the Log.d() and it can read contentModel's data in the Realtime Database. – H.JiMan Aug 29 '19 at 03:29
  • Well, if you're getting the data, then it would seem like an issue in the UI. Are you sure you can see the nested `RecyclerView`s? – Mike M. Aug 29 '19 at 03:44
  • Yes. I connected the adapter to RecyclerView in activity_main.xml. And I checked each view has a title that different each viewType. The only title exists, no data in recyclerView... – H.JiMan Aug 29 '19 at 05:31
  • 1
    @MikeM. Oh, I found it!! Ridiculously, I set the RecyclerView width to 200dp, not wrap_content. So, all the data show up in the app. Thank you! – H.JiMan Aug 29 '19 at 06:46

0 Answers0