Android 购物车(一)

源码:


 clear = (LinearLayout) findViewById(R.id.clear);
        clear.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                data.clear();
                adapter.notifyDataSetChanged();
                integral_sum.setText(0 + "");
                checkBox_select_all.setChecked(false);
              //  checkBox_add.setClickable(false);
            }
        });
        checkBox_select_all = (CheckBox) findViewById(R.id.checkbox_add);
        checkBox_select_all.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                HashMap<Integer, Boolean> isSelected = ShoppingCartAdapter
                        .getIsSelected();
                Iterator iterator = isSelected.entrySet().iterator();
                List<Boolean> array = new ArrayList<Boolean>();//列表中checkbox选中状态
                List<Integer> nums = new ArrayList<Integer>();//列表中商品数量
                while (iterator.hasNext()) {
                    HashMap.Entry entry = (HashMap.Entry) iterator.next();
                    Integer key = (Integer) entry.getKey();
                    Boolean val = (Boolean) entry.getValue();
                    array.add(val);
                }
                for (int i = 0; i < data.size(); i++) {
                    //int num = data.get(i).getNum();
                    int integral = Integer.valueOf(data.get(i).getIntegral());
                   // nums.add(num);
                }
                if (checkBox_select_all.isChecked()) {

                    for (int i = 0; i < data.size(); i++) {
                        ShoppingCartAdapter.getIsSelected().put(i, true);
                    }
                    //checkBox_add.setChecked(true);
                    adapter.notifyDataSetChanged();
                } else {
                    for (int i = 0; i < data.size(); i++) {
                        ShoppingCartAdapter.getIsSelected().put(i, false);
                    }
                    //checkBox_add.setChecked(false);
                    adapter.notifyDataSetChanged();
                    integral_sum.setText(0 + "");
                }

            }
        });
        mListView= (ListView) findViewById(R.id.finance_list);
        mListView.setAdapter(adapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {


                int pos = position - 1;
              //  RecyclerView.ViewHolder viewHolder = (RecyclerView.ViewHolder) view.getTag();
               // int num = data.get(pos).getNum();
                int num = data.get(pos).getNum();
                if (num == 0) {
                    Toast.makeText(context, "请选择商品数量", Toast.LENGTH_LONG)
                            .show();
                } else {
                    boolean cu = !ShoppingCartAdapter.getIsSelected().get(pos);
                    ShoppingCartAdapter.getIsSelected().put(pos, cu);
                    adapter.notifyDataSetChanged();
                    //遍历获取列表中checkbox的选中状态
                    HashMap<Integer, Boolean> isSelected = ShoppingCartAdapter
                            .getIsSelected();
                    Iterator iterator = isSelected.entrySet().iterator();
                    List<Boolean> array = new ArrayList<Boolean>();
                    while (iterator.hasNext()) {
                        HashMap.Entry entry = (HashMap.Entry) iterator.next();
                        Integer key = (Integer) entry.getKey();
                        Boolean val = (Boolean) entry.getValue();
                        array.add(val);
                    }


                    if (!isAllCheck(array)) {
                        checkBox_select_all.setChecked(false);
                        //checkBox_add.setChecked(false);
                    }
                    if (isAllCheck(array)) {
                        checkBox_select_all.setChecked(true);
                        // checkBox_add.setChecked(true);
                    }
//                    if (falsecount==1) {
//                        checkBox_select_all.setChecked(false);
//                    }
//                    if (truecount==1) {
//                        checkBox_add.setChecked(true);
//                    }
                }
            }
        });
    }
    private boolean isAllCheck( List<Boolean> array) {//是否全选
        for (int i=0;i<array.size();i++) {
            if (array.get(i)==false)
                return false;
        }
        return true;
    }
    /**
     * 全选与反选
     */


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @SuppressWarnings("unchecked")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 10){ //更改选中商品的总价格
                float price = (Float)msg.obj;
                if(price > 0){
                    integral_sum.setText(price+"");
                }else{
                    integral_sum.setText("0");
                }
            }
            else if(msg.what == 11){
                //列表选中状态
                List<Boolean> array = (List<Boolean>) msg.obj;
                if (!isAllCheck(array)) {
                    checkBox_select_all.setChecked(false);
                    //checkBox_add.setChecked(false);
                }
                if (isAllCheck(array)) {
                    checkBox_select_all.setChecked(true);
                   // checkBox_add.setChecked(true);
                }
            }
        }
    };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值