Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Nested ArrayList in Android

Posted on November 11, 2022 By

Solution 1 :

I guess you need to header and footer of RecyclerView. Here is the code.

Try this one:

public class HeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    String[] data;

    public HeaderAdapter(String[] data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            //inflate your layout and pass it to view holder
            return new VHItem(null);
        } else if (viewType == TYPE_HEADER) {
            //inflate your layout and pass it to view holder
            return new VHHeader(null);
        }

        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VHItem) {
            String dataItem = getItem(position);
            //cast holder to VHItem and set data
        } else if (holder instanceof VHHeader) {
            //cast holder to VHHeader and set data for header.
        }
    }

    @Override
    public int getItemCount() {
        return data.length + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position))
            return TYPE_HEADER;

        return TYPE_ITEM;
    }

    private boolean isPositionHeader(int position) {
        return position == 0;
    }

    private String getItem(int position) {
        return data[position - 1];
    }

    class VHItem extends RecyclerView.ViewHolder {
        TextView title;

        public VHItem(View itemView) {
            super(itemView);
        }
    }

    class VHHeader extends RecyclerView.ViewHolder {
        Button button;

        public VHHeader(View itemView) {
            super(itemView);
        }
    }
}

Problem :

I want to create a cart which has Nested array.
Like my parent array has items and child array has item’s respective toppings but when i add toppings to my item array it automatically add same toppings to all index of items array. Here is my image:

enter image description here

Here i’m attaching my code below

Item_Array

private void addNewToppinDialoge(ProductsModel.Datum datum, int adapterPosition) {

    ProductOrderModel.Datum productOrderModel = new ProductOrderModel.Datum();
    productOrderModel.setCategoryID(datum.getProductID());
    productOrderModel.setCategory(datum.getProductName());
    int i = productActivity.productOrderModelArrayList.size();
    int j = i + 1;
    productOrderModel.setSrNO(String.valueOf(j));

    productActivity.productOrderModelArrayList.add(productOrderModel);
    productActivity.refreshOrderAdapter();
    productActivity.changeTitleToolbar(datum.getProductName());

}

Toppings_array

private void addToppingToCart(ToppingsModel.Datum.LookupValue lookupValue, int adapterPosition) {
    ProductOrderModel.Datum datum = new ProductOrderModel.Datum();
    ProductOrderModel.Datum.SubCategory subCategory = new ProductOrderModel.Datum.SubCategory();
    subCategory.setCategory(lookupValue.getLookupvalue());

    int pos = productActivity.productOrderModelArrayList.size() - 1;
    Log.e("POS", String.valueOf(ProductActivity.productOrderModelArrayList.size() - 1));

    productActivity.productOrderModelArrayListSub.add(subCategory);
    int subPos = productActivity.productOrderModelArrayListSub.size() - 1;

    productActivity.productOrderModelArrayListSub.get(subPos).setCategory(lookupValue.getLookupvalue());

    productActivity.productOrderModelArrayList.get(pos).setSubCategory(productActivity.productOrderModelArrayListSub);

    productActivity.refreshOrderAdapter();
}

Adapter For Cart

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

    public static ToppingsListAdapter toppingsListAdapter;
    static Context mContext;
    ArrayList<ProductOrderModel.Datum> orderModelArrayList;

    public MyOrderAdapter(Context mContext, ArrayList<ProductOrderModel.Datum> orderModelArrayList) {
        MyOrderAdapter.mContext = mContext;
        this.orderModelArrayList = orderModelArrayList;
    }

    @NonNull
    @Override
    public MyOrderAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.myorder_layout, viewGroup, false);

        return new MyOrderAdapter.ViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyOrderAdapter.ViewHolder holder, int i) {

        holder.mTextOrderName.setText(orderModelArrayList.get(i).getCategory());

        holder.mImageRemove.setOnClickListener(v -> removeItem(orderModelArrayList.get(i), holder.getAdapterPosition()));
        holder.mTextSerialNo.setText(orderModelArrayList.get(i).getSrNO() + ".");

        ArrayList<ProductOrderModel.Datum.SubCategory> arrayItem = (ArrayList<ProductOrderModel.Datum.SubCategory>) orderModelArrayList.get(i).getSubCategory();
        if (arrayItem == null) {

        } else {
            toppingsListAdapter = new ToppingsListAdapter(mContext, arrayItem);
            holder.mRecyclerToppings.setAdapter(toppingsListAdapter);

        }

    }

    protected void removeItem(ProductOrderModel.Datum productOrderModel, int adapterPosition) {
    }

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

    public void refreshAdapter() {
        toppingsListAdapter.notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public RecyclerView mRecyclerToppings;
        private TextView mTextOrderName, mTextSerialNo;
        private ImageView mImageRemove;

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

            mTextOrderName = itemView.findViewById(R.id.orderItemName);
            mImageRemove = itemView.findViewById(R.id.orderRemove);
            mTextSerialNo = itemView.findViewById(R.id.srno);

            mRecyclerToppings = itemView.findViewById(R.id.text_toppings);
            RecyclerView.LayoutManager manager = new LinearLayoutManager(mContext, LinearLayout.VERTICAL, false);
            mRecyclerToppings.setLayoutManager(manager);
        }

        public int getAdapterPosition(ProductOrderModel.Datum.SubCategory remove) {
            return 0;
        }
    }
}

strong text:

READ  [FIXED] android - Admob shows test ads but not real ads in beta version?
Powered by Inline Related Posts

enter image description here

Comments

Comment posted by Aliasgar Patel

Nope this is not the correct way i’m stucked in array not in adapter

Comment posted by Jamil Hasnine Tamim

@AliasgarPatel you should add your toppings array items in footer and and item in header! And in your bindings you frequently re initiate your adapter every time!! Why! it’s should be in your constructor.

Comment posted by Jamil Hasnine Tamim

Problem is in your adapter not in array list.

Comment posted by Aliasgar Patel

I also have 2 nested recyclerview. you can see method @BindViewHolder in Adapter which set another recyclerview adapter

Comment posted by Jamil Hasnine Tamim

@AliasgarPatel this is not actual way brother! Try to use my adapter or like that.

Android Tags:android, android-recyclerview, android-studio, arraylist, java

Post navigation

Previous Post: [FIXED] android – How to pass data by Back Button, Kotlin
Next Post: [FIXED] Project with external module issue in android studio 3.6

Related Posts

[FIXED] android – What is difference between LaunchedEffect parameters? Android
[FIXED] arraylist – Exoplayer with Viewpager2 and recycler view adapter Android
[FIXED] java – How can i store pushy tokens to database Android
[FIXED] java – GOOGLE_APPLICATION_CREDENTIALS can’t be found Android
[FIXED] java – How to insert PDF file along with the app Android
[FIXED] python – Django Rest Framework – How to create a GIF file from an uploaded video Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme