Q: How to work with Nested RecyclerView (Child Item in Parent Item) properly? I have the following code and result:
public class MyClass {
    public static void main(String args[]) {

        String[][] parent_table = {{"I-101", "First(parent)"}, {"I-102", "Second(parent)"}, {"I-103", "Third(parent)"}};
        String[][] child_table = {{"I-101", "First-1(child)"}, {"I-101", "First-2(child)"}, {"I-102", "Second-1(child)"}, {"I-102", "Second-2(child)"}, {"I-103", "Third-1(child)"}};

        for(String[] parent: parent_table) {
            System.out.println(parent[1]);
            for (String[] child: child_table) {
                if (parent[0].equals(child[0])) {
                    System.out.println(child[1]);

                }
            }
        }
    }
}






First(parent)




First-1(child)


First-2(child)


Second-2(child)


Second-1(child)


Second-2(child)


Third(parent)


Third-1(child)




I apply this in nested recyclerview but I'm not getting the actual result, I need the above-mentioned result.

I Need Help to Solve This.
Here is my MainActivity:
package com.example.nestedrecyclerview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    MyAdapter myAdapter;
    ArrayList<ParentItem> parentItemArrayList;
    ArrayList<ChildItem> childItemArrayList;
    RecyclerView RVparent;

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

        RVparent = findViewById(R.id.RVparent);

        String[][] parent_table = {{"I-101", "First(parent)"}, {"I-102", "Second(parent)"}, {"I-103", "Third(parent)"}};
        String[][] child_table = {{"I-101", "First-1(child)"}, {"I-101", "First-2(child)"}, {"I-102", "Second-1(child)"}, {"I-102", "Second-2(child)"}, {"I-103", "Third-1(child)"}};

        parentItemArrayList = new ArrayList<>();
        childItemArrayList = new ArrayList<>();

        for(String[] parent: parent_table) {
            ParentItem parentItem = new ParentItem(parent[1]);
            parentItemArrayList.add(parentItem);
            for (String[] child: child_table) {
                if (parent[0].equals(child[0])) {
                    ChildItem childItem = new ChildItem(child[1]);
                    childItemArrayList.add(childItem);
                }
            }
        }

        myAdapter = new MyAdapter(this,parentItemArrayList,childItemArrayList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        RVparent.setLayoutManager(layoutManager);
        RVparent.setAdapter(myAdapter);

    }


} 

Here is my MyAdapter:
package com.example.nestedrecyclerview;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private Activity activity;
    ArrayList<ParentItem> parentItemArrayList;
    ArrayList<ChildItem> childItemArrayList;

    public MyAdapter(Activity activity, ArrayList<ParentItem> parentItemArrayList, ArrayList<ChildItem> childItemArrayList) {
        this.activity = activity;
        this.parentItemArrayList = parentItemArrayList;
        this.childItemArrayList = childItemArrayList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        //holder.tvName.setText(arrayList.get(position));

        ParentItem parentItem = parentItemArrayList.get(position);

        holder.tvOrderId.setText(parentItem.orderId);

        MemberAdp adapterMember = new MemberAdp(childItemArrayList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        holder.nested_rv.setLayoutManager(linearLayoutManager);
        holder.nested_rv.setAdapter(adapterMember);

    }

    @Override
    public int getItemCount() {

        return parentItemArrayList.size();

    }

    public class ViewHolder extends RecyclerView.ViewHolder {


        TextView tvOrderId, tvqty, tvprice;
        CircleImageView ivParent;
        RecyclerView nested_rv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tvOrderId = itemView.findViewById(R.id.tvOrderId);
            nested_rv = itemView.findViewById(R.id.nested_rv);

        }
    }

}


Here is my MenberAdp:
package com.example.nestedrecyclerview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class MemberAdp extends RecyclerView.Adapter<MemberAdp.ViewHolder> {

    ArrayList<ChildItem> childarrayList;

    public MemberAdp(ArrayList<ChildItem> childarrayList) {

        this.childarrayList = childarrayList;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_item,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        ChildItem childItem = childarrayList.get(position);

        holder.tvItemName.setText(childItem.itemName);

    }

    @Override
    public int getItemCount() {

        return childarrayList.size();

    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        TextView tvItemName, tvqtyChild, tvpriceChild;
        CircleImageView ivChild;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tvItemName = itemView.findViewById(R.id.tvItemName);
        }
    }

}


