商品详情页面,点击按钮将对应的商品加入购物车

商品详情页面是通过 点击首页的商品,传当前商品的信息到商品详情页面展示,

点击 加入购物车的按钮 将当前商品加入到购物车里

这是首页的商品展示



点击商品 跳转到商品详情页面


商品详情页面的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.jdong.CustomXiangQiangActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#F5F5F5"
        android:gravity="center_vertical"
        android:padding="10dp">

        <ImageView
            android:id="@+id/custom_fanhui"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/aa4" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="商品详情"
            android:textSize="23sp" />
    </RelativeLayout>
    <!-- fresco:failureImage="@drawable/tuijian"
   fresco:placeholderImage="@drawable/tuijian"-->

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/custom_xq_viewpager"
            android:layout_width="match_parent"
            android:layout_height="560dp" />

        <TextView
            android:id="@+id/custom_xq_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:paddingLeft="20dp"
            android:text="Apple iPhone 8 Plus"
            android:textColor="#000"
            android:textSize="23sp" />

        <TextView
            android:id="@+id/custom_xq_bargin_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:padding="10dp"
            android:paddingLeft="30dp"
            android:text="¥6688.00"
            android:textColor="#000"
            android:textSize="23sp" />

        <TextView
            android:id="@+id/custom_xq_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:padding="10dp"
            android:paddingLeft="30dp"
            android:text="¥6688.00"
            android:textColor="#F23030"
            android:textSize="26sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="供应商"
            android:textSize="18sp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="联系卖家"
            android:textSize="18sp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="进入店铺"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/jiagou_btn"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#f00"
            android:gravity="center"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:text="加入购物车"
            android:textColor="#fff"
            android:textSize="19sp" />

    </LinearLayout>
</LinearLayout>
详情CustomXiangQingActivity.java的代码

public class CustomXiangQiangActivity extends AppCompatActivity implements AddCartViewCallBack{

    @BindView(R.id.custom_fanhui)
    ImageView customFanhui;
    @BindView(R.id.custom_xq_viewpager)
    ViewPager customXqViewpager;
    @BindView(R.id.custom_xq_title)
    TextView customXqTitle;
    @BindView(R.id.custom_xq_bargin_price)
    TextView customXqBarginPrice;
    @BindView(R.id.custom_xq_price)
    TextView customXqPrice;
    @BindView(R.id.jiagou_btn)
    TextView jiagouBtn;
    private AddCartPresenter addCartPresenter;
    private String pid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_xing_qing);
        ButterKnife.bind(this);
        //https://www.zhaoapi.cn/product/addCart
        //添加购物车

        //拿到传来的参数
        Intent intent = getIntent();
        //images,pid,bargainPrice,title,price
        pid = intent.getStringExtra("pid");
        String images = intent.getStringExtra("images");
        String bargainPrice = intent.getStringExtra("bargainPrice");
        String title = intent.getStringExtra("title");
        String price = intent.getStringExtra("price");


        //原价设置删除线
        customXqPrice.setText("¥"+price+"");
        customXqTitle.setText(title+"");
        customXqBarginPrice.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
        customXqBarginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中间横线(删除线)
        customXqBarginPrice.getPaint().setAntiAlias(true);// 抗锯齿
        customXqBarginPrice.setText("¥"+bargainPrice+"");

        List<String> listImage = new ArrayList<>();

        //图片的集合
        if(images.contains("|")){
            //如果需要拆分
            String[] split = images.split("\\|");
            for (int i=0;i<split.length;i++){
                listImage.add(split[0]);
            }
        }else{
            listImage.add(images);
        }

        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
        viewPagerAdapter.addData(listImage);
        customXqViewpager.setAdapter(viewPagerAdapter);

        addCartPresenter = new AddCartPresenter(this);

    }

    @OnClick({R.id.custom_fanhui, R.id.custom_xq_viewpager, R.id.custom_xq_title, R.id.custom_xq_bargin_price, R.id.custom_xq_price, R.id.jiagou_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.custom_fanhui:
                finish();
                break;

            case R.id.jiagou_btn://点击加入购物车,动态添加商品根据pid,
               // https://www.zhaoapi.cn/product/addCart
                //"uid": 1650,
                // "token": "2FC3EF31EA25696D2715A971ADE38DE1",
                 addCartPresenter.getData(pid);
                break;
        }
    }

    @Override
    public void success(AddCartBean addCartBean) {

         Toast.makeText(this,""+addCartBean.getMsg(),Toast.LENGTH_LONG).show();
    }

    @Override
    public void failure() {

    }
}
AddCartPresenter层

public class AddCartPresenter {
    AddCartModel addCartModel = new AddCartModel();

    AddCartViewCallBack addCartViewCallBack;
    public AddCartPresenter(AddCartViewCallBack addCartViewCallBack) {
        this.addCartViewCallBack = addCartViewCallBack;
    }

    public void getData(String pid) {

        addCartModel.getData(pid, new AddCartModelCallBack() {
            @Override
            public void success(AddCartBean addCartBean) {
                addCartViewCallBack.success(addCartBean);
            }

            @Override
            public void failure() {
                addCartViewCallBack.failure();
            }
        });

    }
}
AddCartModel层请求网络数据

public class AddCartModel {

  public void getData(String pid, final AddCartModelCallBack addCartModelCallBack) {
      // https://www.zhaoapi.cn/product/addCart?source=android&uid=1650&pid=57
      //"uid": 1650,
      // "token": "2FC3EF31EA25696D2715A971ADE38DE1",
      //"pid":57
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://www.zhaoapi.cn")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        IGetDataService service = retrofit.create(IGetDataService.class);

        Map<String,String> map = new HashMap<>();
        map.put("source","android");
        map.put("uid","1650");
        map.put("token","2FC3EF31EA25696D2715A971ADE38DE1");
        map.put("pid",pid);

      service.addCart(map).enqueue(new Callback<AddCartBean>() {
            @Override
            public void onResponse(Call<AddCartBean> call, Response<AddCartBean> response) {
                AddCartBean addCartBean = response.body();
                addCartModelCallBack.success(addCartBean);
            }

            @Override
            public void onFailure(Call<AddCartBean> call, Throwable t) {
                addCartModelCallBack.failure();
            }
        });
    }
}

这样就完成了动态添加购物车



  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以帮您提供参考代码,但请注意以下几点: 1. 由于涉及到具体业务逻辑,代码仅供参考,需要您根据实际情况进行修改。 2. 代码中的数据库操作、接口地址、实体类等需要根据您的具体情况进行修改。 3. 代码中的前端页面需要您进行美化和适配。 以下是参考代码: 1. 前端页面商品详情页中添加一个按钮点击按钮即可将商品加入购物车按钮代码如下: ``` <button id="add-to-cart-btn" onclick="addToCart()">加入购物车</button> ``` 2. 后台功能代码 (1)首先需要定义一个购物车实体类,用于存储用户加入购物车商品信息。 ``` public class CartItem { private Long id; // 商品ID private String name; // 商品名称 private BigDecimal price; // 商品价格 private Integer quantity; // 商品数量 // getter和setter方法 } ``` (2)定义一个购物车服务接口,用于提供加入购物车、查询购物车等功能。 ``` public interface CartService { void addToCart(Long itemId, Integer quantity); // 将商品加入购物车 List<CartItem> getCartItems(); // 查询购物车中的商品列表 } ``` (3)实现购物车服务接口,具体实现代码如下: ``` @Service public class CartServiceImpl implements CartService { @Autowired private CartItemMapper cartItemMapper; // 购物车数据访问层接口 @Override public void addToCart(Long itemId, Integer quantity) { // 查询商品信息 Item item = itemMapper.selectByPrimaryKey(itemId); if (item == null) { throw new RuntimeException("商品不存在"); } // 创建购物车项 CartItem cartItem = new CartItem(); cartItem.setId(itemId); cartItem.setName(item.getName()); cartItem.setPrice(item.getPrice()); cartItem.setQuantity(quantity); // 将购物车项插入到数据库中 cartItemMapper.insert(cartItem); } @Override public List<CartItem> getCartItems() { // 查询购物车中的商品列表 return cartItemMapper.selectAll(); } } ``` (4)定义一个控制器,用于处理加入购物车的请求。 ``` @RestController public class CartController { @Autowired private CartService cartService; @PostMapping("/cart/add") public void addToCart(@RequestParam("itemId") Long itemId, @RequestParam("quantity") Integer quantity) { cartService.addToCart(itemId, quantity); } } ``` 这样,当用户在商品详情点击加入购物车按钮时,就会发送一个POST请求到“/cart/add”接口,将商品加入购物车。 希望这些代码能够对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值