Android中的嵌套ArrayList

Android中的嵌套ArrayList

我想创建一个具有Nested array的购物车。
就像我的父数组具有项,子数组具有项的相应浇头一样,但是当我向我的项数组中添加浇头时,它会自动为所有项数组索引添加相同的浇头。这是我的图像:

java - Android中的嵌套ArrayList-LMLPHP

我在这里附上我的代码

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


强文本:

java - Android中的嵌套ArrayList-LMLPHP

最佳答案

我想您需要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/

10-10 18:05