购物车操作增删改查

MainActivity
private TextView shoppingcart_text;
    private RelativeLayout relativelayout;
    private ExpandableListView expandableListView;
    private CheckBox checkAll;
    private TextView sun;
    private Button shancuh;
    private Button goumai;
    private RelativeLayout relative;
    private GetCartPresenter getCartPresenter;
    private QueryBean queryBean;
    //定义handler
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                countPriceBean = (CountPriceBean) msg.obj;
                //设置总金额
                sun.setText(countPriceBean.getPriceString());
            }else if(msg.what==1){
                QueryBean queryBean = (QueryBean) msg.obj;
                Log.d("TAG","Tag+++++++++++++"+queryBean);


                //判断当前所有子条目是否选中来决定一组的选中状态
                for (int i = 0; i < queryBean.getData().size(); i++) {
                    if (ischilditemcheck(i)) {
                        //如果子条目都选中为,true
                        queryBean.getData().get(i).setGroupChecked(true);
                    }
                }

                //判断所有组是不是选中,来显示全选是不是选中
                checkAll.setChecked(ischeclAll());


            //设置适配器
                if (queryBean!=null){

                myAdapter = new MyAdapter(MainActivity.this, queryBean,handler,getCartPresenter,sp);
            expandableListView.setAdapter(myAdapter);
                }

            //展开组
                for (int i=0;i<queryBean.getData().size();i++){
                    expandableListView.expandGroup(i);
                }

            //发送价钱做显示
            myAdapter.sendPriceAndCount();

            }

        }
    };
    private CountPriceBean countPriceBean;
    private MyAdapter myAdapter;
    private SharedPreferences sp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //去掉默认指示器
        expandableListView.setGroupIndicator(null);
    }

    private void initView() {
        sp = getSharedPreferences("bianji_flag", MODE_PRIVATE);
        boolean flag = sp.getBoolean("flag", false);
        if(!flag){
            SharedPreferences.Editor edit = sp.edit();
            edit.putBoolean("flag",true);
            edit.commit();

        };

        shoppingcart_text = (TextView) findViewById(R.id.shoppingcart_text);
        relativelayout = (RelativeLayout) findViewById(R.id.relativelayout);
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        checkAll = (CheckBox) findViewById(R.id.checkAll);
        sun = (TextView) findViewById(R.id.sun);
        shancuh = (Button) findViewById(R.id.shancuh);
        goumai = (Button) findViewById(R.id.goumai);
        relative = (RelativeLayout) findViewById(R.id.relative);

        shancuh.setOnClickListener(this);
        goumai.setOnClickListener(this);
        checkAll.setOnClickListener(this);

        getCartPresenter = new GetCartPresenter(MainActivity.this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        //请求网络
        getNetWork();


    }

    private void getNetWork() {
        getCartPresenter.getNetWrok();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shancuh:
                for (int i=0;i<queryBean.getData().size();i++){
                    for (int j=0;j<queryBean.getData().get(i).getList().size();j++){
                        if (queryBean.getData().get(i).getList().get(j).getSelected()==1){
                            Map<String,String> params = new HashMap<>();
                            params.put("uid","4575");
                            params.put("pid", String.valueOf(queryBean.getData().get(i).getList().get(j).getPid()));

                            RetrofitUtil.delcards(params, new Callback<DelCardBean>() {
                                @Override
                                public void onResponse(Call<DelCardBean> call, Response<DelCardBean> response) {
                                    if (response.isSuccessful()){
                                        getCartPresenter.getNetWrok();
                                    }
                                }

                                @Override
                                public void onFailure(Call<DelCardBean> call, Throwable t) {

                                }
                            });
                        }
                    }
                }

                break;
            case R.id.goumai:

                break;
            case R.id.checkAll:
                Log.i("tag","tagtagtagtagtagtagtagtagtag");
                myAdapter.setAllChildState(checkAll.isChecked());
                ischeclAll();
                break;
        }
    }

    @Override
    public void onSeccess(QueryBean queryBean) {
        if (queryBean!=null){
        this.queryBean = queryBean;
        Message msg = Message.obtain();
        msg.what=1;
        msg.obj=queryBean;
        handler.sendMessage(msg);

        }else{
            Toast.makeText(MainActivity.this,"购物车为空",Toast.LENGTH_SHORT).show();
        }
    }

    //判断当前子条目的选中状态
    private boolean ischilditemcheck(int groupposition) {
        for (int i=0;i<queryBean.getData().get(groupposition).getList().size();i++){
            if (queryBean.getData().get(groupposition).getList().get(i).getSelected()==0){
                return false;
            }
        }
        return true;
    }

    //判断所有组是不是选中
    private boolean ischeclAll() {
        for (int i=0;i<queryBean.getData().size();i++){
            if (queryBean.getData().get(i).isGroupChecked()==false){
                return false;
            }
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean("flag",true);
        edit.commit();
    }

Activity布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.luobing.jinjieweek2.view.MainActivity">

    <RelativeLayout
        android:id="@+id/relativelayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        >
        <TextView
            android:padding="5dp"
            android:layout_centerHorizontal="true"
            an
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值