购物车全选,总价

这个博客展示了如何在Android应用中实现购物车的全选功能,并计算总价。通过使用`CartBean`来存储商品信息,`UserAdapter`适配器展示购物车内容,`MainActivity`作为视图接口,实现了`CartContract`接口。应用中还引入了`XRecyclerView`库进行列表展示,并使用`CheckBox`处理全选事件,结合`CompoundButton`监听单个商品选择状态。
摘要由CSDN通过智能技术生成

package com.example.aason.gouwuche1.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.example.aason.gouwuche1.R;
import com.example.aason.gouwuche1.adapter.UserAdapter;
import com.example.aason.gouwuche1.bean.CartBean;
import com.example.aason.gouwuche1.contract.CartContract;
import com.example.aason.gouwuche1.presenter.CartPresenter;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity implements CartContract.ICartView {

private XRecyclerView rev;
private Button btn;
private CheckBox ck;
private CartPresenter cartPresenter;
private UserAdapter userAdapter;
public List<CartBean.DataBean> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rev = findViewById(R.id.rev);
    btn = findViewById(R.id.btn);
    ck = findViewById(R.id.ck);
    list = new ArrayList<>();
    cartPresenter = new CartPresenter(this);
    cartPresenter.getCart(new HashMap<String, String>());
    ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                for(CartBean.DataBean dataBean:list){
                    dataBean.isChecked=true;
                    for(CartBean.DataBean.SpusBean spusBean:dataBean.getSpus()){
                        spusBean.isProductChecked=true;
                    }
                }
            }else{
                for(CartBean.DataBean dataBean:list){
                    dataBean.isChecked=false;
                    for(CartBean.DataBean.SpusBean spusBean:dataBean.getSpus()){
                        spusBean.isProductChecked=false;
                    }
                }
            }
         userAdapter.notifyDataSetChanged();
            getTouchprice();
        }
    });
}

@Override
public void success(List<CartBean.DataBean> list1) {
    if (list1 != null) {
        list=list1;
    }
    userAdapter = new UserAdapter(this,list);
    rev.setLayoutManager(new LinearLayoutManager(this));
    rev.setAdapter(userAdapter);
}

@Override
public void fail(String msg) {

}
private void getTouchprice() {
    double touchprice=0;
    for(CartBean.DataBean dataBean:list){
        for(CartBean.DataBean.SpusBean spusBean:dataBean.getSpus()){
            if (spusBean.isProductChecked) {
                touchprice+=spusBean.getPraise_num();
            }
        }
    }
    ck.setText("¥:"+touchprice);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在UniApp中实现购物车全选、单选和反选功能,可以通过以下步骤进行操作: 1. 数据结构设计:首先,你需要定义一个购物车数据结构,可以使用数组或对象来表示每个商品的信息,例如商品名称、价格、数量等。同时,为每个商品添加一个选中状态的属性,用于记录是否被选中。 2. 渲染购物车列表:在页面中使用`v-for`指令遍历购物车数据,并将商品信息展示出来。同时,为每个商品的选中状态绑定一个`v-model`指令,用于实现单选功能。 3. 实现全选功能:添加一个全选的复选框,通过绑定一个变量来控制全选的状态。当全选复选框被点击时,遍历购物车数据,将每个商品的选中状态与全选状态保持一致。 4. 实现反选功能:添加一个反选按钮,当点击反选按钮时,遍历购物车数据,将每个商品的选中状态取反。 5. 计算总价和总数量:通过遍历购物车数据,累加选中商品的价格和数量,得到总价和总数量。 6. 相关代码示例: ```html <template> <div> <div> <input type="checkbox" v-model="selectAll" @change="handleSelectAll" /> 全选 <button @click="handleInverseSelect">反选</button> </div> <div v-for="(item, index) in cartList" :key="index"> <input type="checkbox" v-model="item.selected" @change="handleSelectItem(index)" /> {{ item.name }} - ¥{{ item.price }} - 数量:{{ item.quantity }} </div> <div> 总价:¥{{ totalPrice }},总数量:{{ totalQuantity }} </div> </div> </template> <script> export default { data() { return { selectAll: false, cartList: [ { name: '商品1', price: 10, quantity: 1, selected: false }, { name: '商品2', price: 20, quantity: 2, selected: false }, { name: '商品3', price: 30, quantity: 3, selected: false } ] }; }, computed: { totalPrice() { let total = 0; for (let item of this.cartList) { if (item.selected) { total += item.price * item.quantity; } } return total; }, totalQuantity() { let total = 0; for (let item of this.cartList) { if (item.selected) { total += item.quantity; } } return total; } }, methods: { handleSelectAll() { for (let item of this.cartList) { item.selected = this.selectAll; } }, handleSelectItem(index) { this.cartList[index].selected = !this.cartList[index].selected; }, handleInverseSelect() { for (let item of this.cartList) { item.selected = !item.selected; } } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值