Skip to content Skip to sidebar Skip to footer

Problems With Nested Recylerview

I have data from Firebase data like below: My problem is my RecyclerView display not as expected. The data of my messages were accumulated, it should go one part by one part and s

Solution 1:

It's hard to point at one specific place and say "this is the issue," since there's a lot of code. But it boils down to the fact that there's only a single List<QuestionsToTeacher> questionsToTeacherList inside AdapterAskTeacherList.

Instead, you should have a List<List<QuestionsToTeacher>>. That is, a list of lists.

To accomplish this, you'll have to change both the Activity and the outer Adapter. In the activity, change this:

public ArrayList<QuestionsToTeacher> questionsToTeacherList = new ArrayList<>();

...

for(DataSnapshot messagesLabel: subChapterData.getChildren())
{
    //Messages Data
    for(DataSnapshot messagesData: messagesLabel.getChildren())
    {
        String messageQuestion = gson.toJson(messagesData.getValue());
        QuestionsToTeacher questionsToTeacher = gson.fromJson(messageQuestion,QuestionsToTeacher.class);

        //Problem start here
        questionsToTeacherList.add(questionsToTeacher); 
        System.out.println(questionsToTeacher.messages+"    "+questionsToTeacher.questionStatus);
    }
}

to this:

public ArrayList<ArrayList<QuestionsToTeacher>> questionsToTeacherList = new ArrayList<>();

...

for(DataSnapshot messagesLabel: subChapterData.getChildren())
{
    ArrayList<QuestionsToTeacher> sublist = new ArrayList<>();
    //Messages Data
    for(DataSnapshot messagesData: messagesLabel.getChildren())
    {
        String messageQuestion = gson.toJson(messagesData.getValue());
        QuestionsToTeacher questionsToTeacher = gson.fromJson(messageQuestion,QuestionsToTeacher.class);

        //Problem start here
        sublist.add(questionsToTeacher); 
        System.out.println(questionsToTeacher.messages+"    "+questionsToTeacher.questionStatus);
    }
    questionsToTeacherList.add(sublist);
}

What you're doing here is taking the ArrayList<QuestionsToTeacher> (the list of questions) and changing it to an ArrayList<ArrayList<QuestionsToTeacher>> (a list of lists of questions). And then, as you read the data out of the database, you create multiple ArrayList<QuestionsToTeacher> and add them to the list of lists.

Then, in your adapter, change this:

private List<QuestionsToTeacher> questionsToTeacherList = new ArrayList<>();

public AdapterAskTeacherList(List<AskTeacherItems> askTeacherListItems, List<QuestionsToTeacher> questionsToTeacherList, Context context) {
    ...
    this.questionsToTeacherList = questionsToTeacherList;
    ...
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ...
        AdapterQuestionToTeacher adapterQuestionToTeacher = new AdapterQuestionToTeacher(questionsToTeacherList);
        view.recyclerView.setAdapter(adapterQuestionToTeacher);
        ...
}

to this:

private ArrayList<ArrayList<QuestionsToTeacher>> questionsToTeacherList = new ArrayList<>();

public AdapterAskTeacherList(List<AskTeacherItems> askTeacherListItems, ArrayList<ArrayList<QuestionsToTeacher>> questionsToTeacherList, Context context) {
    this.questionsToTeacherList = questionsToTeacherList;
    ...
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ...
        List<QuestionsToTeacher> sublist = questionsToTeacherList.get(position);
        AdapterQuestionToTeacher adapterQuestionToTeacher = new AdapterQuestionToTeacher(sublist);
        view.recyclerView.setAdapter(adapterQuestionToTeacher);
        ...
}

Again, you're changing the adapter to accept a list of lists of questions (instead of just a list of questions). The key is that you then pass only a single list of questions to each nested RecyclerView.


Post a Comment for "Problems With Nested Recylerview"