java - Android 中的嵌套 ArrayList

标签 java android android-studio arraylist android-recyclerview

我想创建一个具有嵌套数组的购物车。 就像我的父数组有项目,子数组有项目各自的配料,但是当我向项目数组添加配料时,它会自动向项目数组的所有索引添加相同的配料。这是我的图片:

enter image description here

我在下面附上我的代码

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();
}

推车适配器

 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;
        }
    }
}

强文本:

enter image description here

最佳答案

我猜你需要RecyclerView的页眉和页脚。这是代码。

试试这个:

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);
        }
    }
}

关于java - Android 中的嵌套 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60407003/

相关文章:

java - 交叉编译器与 JVM

java - 如何使用ARCore识别盒子?

Android 二维图形库

java - 将内部 HashMap 类型强制转换为 Map

java - XSLT 收集数据

java - 许多十六进制字符的 NumberFormatException

android-studio - Gradle:viewBinding 的参数太多

android-studio - 如何解决在android studio中创建新项目时出现hamcrest jar文件错误

java.lang.NoClassDefFoundError : java. awt.Point

java - 跟进: Create an an array of objects from classname