0

I want to show inner json array in Recyclerview.
I was getting all the data in logcat.
But my problem is how to write code in adapter class for inner array items.
My problem is like this question
How to get data from nested json array android

I have similar json array.
I want to pass arraylist to Adapter.

This is my modal

public class OuterModal {
String menuId;
String menuName;
private List<InnerModal> innerItem;

public String getMenuId() {
    return menuId;
}

public void setMenuId(String menuId) {
    this.menuId = menuId;
}

public String getMenuName() {
    return menuName;
}

public void setMenuName(String menuName) {
    this.menuName = menuName;
}

public List<InnerModal> getInnerItem() {
    return innerItem;
}

public void setInnerItem(List<InnerModal> innerItem) {
    this.innerItem = innerItem;
}

}

This is my another modal

public class InnerModal {
String vendor_item_id;
String vendor_id;
String vendor_item_name;
String vendor_item_image;
String vendor_item_price;
String vendor_item_category;
String vendor_item_description;
String vendor_item_status;

public String getVendor_item_id() {
    return vendor_item_id;
}

public void setVendor_item_id(String vendor_item_id) {
    this.vendor_item_id = vendor_item_id;
}

public String getVendor_id() {
    return vendor_id;
}

public void setVendor_id(String vendor_id) {
    this.vendor_id = vendor_id;
}

public String getVendor_item_name() {
    return vendor_item_name;
}

public void setVendor_item_name(String vendor_item_name) {
    this.vendor_item_name = vendor_item_name;
}

public String getVendor_item_image() {
    return vendor_item_image;
}

public void setVendor_item_image(String vendor_item_image) {
    this.vendor_item_image = vendor_item_image;
}

public String getVendor_item_price() {
    return vendor_item_price;
}

public void setVendor_item_price(String vendor_item_price) {
    this.vendor_item_price = vendor_item_price;
}

public String getVendor_item_category() {
    return vendor_item_category;
}

public void setVendor_item_category(String vendor_item_category) {
    this.vendor_item_category = vendor_item_category;
}

public String getVendor_item_description() {
    return vendor_item_description;
}

public void setVendor_item_description(String vendor_item_description) {
    this.vendor_item_description = vendor_item_description;
}

public String getVendor_item_status() {
    return vendor_item_status;
}

public void setVendor_item_status(String vendor_item_status) {
    this.vendor_item_status = vendor_item_status;
}

}

This is JSON Responce

 StringRequest stringRequest = new StringRequest(Request.Method.GET,
                        mainUrl, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = jsonObject.getJSONArray("menu");
                            List<OuterModal> outerModalList=new ArrayList<>();
                            OuterModal outerModal=new OuterModal();
                            for (int i = 0; i < jsonArray.length(); i++) {

                                JSONObject json = jsonArray.getJSONObject(i);
                                String menu_id = json.getString("menu_id");
                                String menu_name = json.getString("menu_name");
                                outerModal.setMenuId(menu_id);
                                outerModal.setMenuName(menu_name);
                                JSONArray productsListArray = json.getJSONArray("Products List");
                                for (int j = 0; j < productsListArray.length(); j++) {

                                   // Log.d("myDraaa", "list responce:" + productsListArray);

                                    JSONObject productsListjson = productsListArray.getJSONObject(j);
                                    InnerModal innerModal=new InnerModal();
                                    List<InnerModal> innerModalList=new ArrayList<>();
                                    String vendor_item_id = productsListjson.getString("vendor_item_id");
                                    String vendor_id = productsListjson.getString("vendor_id");
                                    String vendor_item_name = productsListjson.getString("vendor_item_name");
                                    String vendor_item_image = productsListjson.getString("vendor_item_image");
                                    String vendor_item_status = productsListjson.getString("vendor_item_status");
                                    String quantity = productsListjson.getString("quantity");
                                    innerModal.setVendor_id(vendor_id);
                                    innerModal.setVendor_item_id(vendor_item_id);
                                    innerModal.setVendor_item_category("");
                                    innerModal.setVendor_item_description("");
                                    innerModal.setVendor_item_image(vendor_item_image);
                                    innerModal.setVendor_item_name(vendor_item_name);
                                    innerModal.setVendor_item_status(vendor_item_status);
                                    innerModal.setVendor_item_price(quantity);

                                    outerModal.getInnerItem().add(innerModal);
                                }
                                outerModalList.add(outerModal);
                            }


public class SecondAdapter extends RecyclerView.Adapter<SecondAdapter.SecondVH> {
    List<OuterModal> arrayList;
    Context context;
    public SecondAdapter(List<OuterModal> arrayList, Context context) {
        this.arrayList = arrayList;
        this.context = context;
    }


    @NonNull
    @Override
    public SecondVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.my_custom_style, null, false);
        return new SecondVH(v);
    }

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

        OuterModal outerModal=arrayList.get(position);
        holder.title.setText(outerModal.getMenuName());

    }

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

    public class SecondVH extends RecyclerView.ViewHolder {
        TextView title;
        TextView items;
        ConstraintLayout constraintLayout;

        TextView itemName, itemPrice, itemDescription;


        public SecondVH(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.bestSellers);
            items = itemView.findViewById(R.id.myCustItemName);
            constraintLayout = itemView.findViewById(R.id.csL);
            itemPrice = itemView.findViewById(R.id.itemPrice);
            itemDescription = itemView.findViewById(R.id.itemDesc);
        }
    }
}

Thanks in advance.
Please help me.

10
  • Please add your code what you have tried and errors which you get. you can remove url for security reasons. and add a json data response Commented Jan 25, 2019 at 7:13
  • simply create Model class for that inner array and create its list and pass it to Adapter. kindly add json response. Commented Jan 25, 2019 at 7:18
  • I have same type url Commented Jan 25, 2019 at 7:19
  • stackoverflow.com/questions/45277540/… Commented Jan 25, 2019 at 7:19
  • Please see my code above Commented Jan 25, 2019 at 7:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.