GWWWWWWWEEEE

public class GwAdapter extends BaseExpandableListAdapter {
    private final Handler handler;
    private final GWPersent gwPersent;
    private List<GWBean.DataBean> data;
    private Context context;
    private int childIndex;
    private int allIndex;

    private RelativeLayout relative_progress;

    public GwAdapter(List<GWBean.DataBean> data, Context context, Handler handler, GWPersent gwPersent, RelativeLayout relative_progress) {
        this.data=data;
        this.context=context;
        this.gwPersent=gwPersent;
        this.handler=handler;
        this.relative_progress=relative_progress;
    }

    @Override
    public int getGroupCount() {
        return data.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final MyGroupHolder myGroupHolder;
        if (convertView==null){
            convertView=View.inflate(context, R.layout.gw_group,null);
            myGroupHolder=new MyGroupHolder();
            myGroupHolder.checkBox= (CheckBox) convertView.findViewById(R.id.group_check);
            myGroupHolder.textView= (TextView) convertView.findViewById(R.id.group_text);
            convertView.setTag(myGroupHolder);
        }else {
            myGroupHolder= (MyGroupHolder) convertView.getTag();
        }
        myGroupHolder.textView.setText(data.get(groupPosition).getSellerName());
        myGroupHolder.checkBox.setChecked(data.get(groupPosition).isGroupChecked());
        final GWBean.DataBean dataBean = data.get(groupPosition);
        myGroupHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                relative_progress.setVisibility(View.VISIBLE);
                childIndex=0;
                updateAllChildInGroup(dataBean,myGroupHolder.checkBox.isChecked());
            }
        });
        return convertView;
    }
    /**
     * 根据商家状态使用递归改变所有的子条目
     */
    private void updateAllChildInGroup(final GWBean.DataBean dataBean, final boolean checked) {
        GWBean.DataBean.ListBean listBean = dataBean.getList().get(childIndex);
        Map<String, String> params = new HashMap<>();

        //?uid=71&sellerid=1&pid=1&selected=0&num=10
        params.put("uid","3410");
        params.put("sellerid", String.valueOf(listBean.getSellerid()));
        params.put("pid", String.valueOf(listBean.getPid()));

        params.put("selected", String.valueOf(checked ? 1:0));
        params.put("num", String.valueOf(listBean.getNum()));

        OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //更新成功一条
                if (response.isSuccessful()){
                    //索引增加
                    childIndex ++;
                    if (childIndex < dataBean.getList().size()){
                        //再去更新下一条
                        updateAllChildInGroup(dataBean,checked);

                    }else {//全都更新完成了....重新查询购物车
                        gwPersent.setUrl(ApiUtil.cartUrl);
                    }
                }

            }
        });
    }





    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final MyChildHolder myChildHolder;
        if (convertView==null){
            convertView=View.inflate(context,R.layout.gw_child,null);
            myChildHolder=new MyChildHolder();
            myChildHolder.checkBox= (CheckBox) convertView.findViewById(R.id.child_check);
            myChildHolder.imageView= (ImageView) convertView.findViewById(R.id.child_image);
            myChildHolder.text_add= (TextView) convertView.findViewById(R.id.text_add);
            myChildHolder.text_jian= (TextView) convertView.findViewById(R.id.text_jian);
            myChildHolder.text_title= (TextView) convertView.findViewById(R.id.child_title);
           myChildHolder.text_price= (TextView) convertView.findViewById(R.id.child_price);
            myChildHolder.text_num= (TextView) convertView.findViewById(R.id.text_num);
            myChildHolder.text_delete= (TextView) convertView.findViewById(R.id.text_delete);
            convertView.setTag(myChildHolder);
        }else {
            myChildHolder= (MyChildHolder) convertView.getTag();
        }
        final GWBean.DataBean.ListBean listBean = data.get(groupPosition).getList().get(childPosition);
        myChildHolder.text_title.setText(listBean.getTitle());
        myChildHolder.text_num.setText(listBean.getNum()+"");
        myChildHolder.text_price.setText(listBean.getPrice()+"");
        String images = listBean.getImages();
        String[] split = images.split("\\|");
        Glide.with(context).load(split[0]).into(myChildHolder.imageView);
        final GWBean.DataBean.ListBean listBean1 = data.get(groupPosition).getList().get(childPosition);
        myChildHolder.checkBox.setChecked(listBean.getSelected()==0?false:true);
        myChildHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //此时需要显示进度条
                relative_progress.setVisibility(View.VISIBLE);
                //更新购物车,,,,需要改变是否选中,,,如果现在显示的是0,改成1;;;1--->0

                Map<String, String> params = new HashMap<>();

                //?uid=71&sellerid=1&pid=1&selected=0&num=10
                params.put("uid","3410");
                params.put("sellerid", String.valueOf(listBean1.getSellerid()));
                params.put("pid", String.valueOf(listBean1.getPid()));

                params.put("selected", String.valueOf(listBean1.getSelected() == 0? 1:0));
                params.put("num", String.valueOf(listBean1.getNum()));

                OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //更新成功之后,,,,再次查询购物车的数据进行展示
                        if (response.isSuccessful()){
                            gwPersent.setUrl(ApiUtil.cartUrl);
                        }
                    }
                });
            }
        });

        //加号
        myChildHolder.text_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //此时需要显示进度条
                relative_progress.setVisibility(View.VISIBLE);
                //更新购物车,,,,需要改变是数量,,,,需要加1

                Map<String, String> params = new HashMap<>();

                //?uid=71&sellerid=1&pid=1&selected=0&num=10
                params.put("uid","3410");
                params.put("sellerid", String.valueOf(listBean1.getSellerid()));
                params.put("pid", String.valueOf(listBean1.getPid()));
                params.put("selected", String.valueOf(listBean1.getSelected()));

                params.put("num", String.valueOf(listBean1.getNum()+1));

                OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //更新成功之后,,,,再次查询购物车的数据进行展示
                        if (response.isSuccessful()){
                            gwPersent.setUrl(ApiUtil.cartUrl);
                        }
                    }
                });

            }
        });
        //减号
        myChildHolder.text_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = listBean1.getNum();

                if (num == 1){
                    return;
                }

                //此时需要显示进度条
                relative_progress.setVisibility(View.VISIBLE);
                //更新购物车,,,,需要改变是数量,,,,需要加1

                Map<String, String> params = new HashMap<>();

                //?uid=71&sellerid=1&pid=1&selected=0&num=10
                params.put("uid","3410");
                params.put("sellerid", String.valueOf(listBean1.getSellerid()));
                params.put("pid", String.valueOf(listBean1.getPid()));
                params.put("selected", String.valueOf(listBean1.getSelected()));

                params.put("num", String.valueOf(num-1));

                OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //更新成功之后,,,,再次查询购物车的数据进行展示
                        if (response.isSuccessful()){
                            gwPersent.setUrl(ApiUtil.cartUrl);
                        }
                    }
                });
            }
        });

        //删除
        myChildHolder.text_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //显示进度条
                relative_progress.setVisibility(View.VISIBLE);
                //调用删除的接口
                Map<String, String> params = new HashMap<>();

                //uid=72&pid=1
                params.put("uid","3410");
                params.put("pid", String.valueOf(listBean1.getPid()));

                OkHttp3Util.doPost(ApiUtil.deleteCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()){
                            //再次请求购物车的数据
                            gwPersent.setUrl(ApiUtil.cartUrl);
                        }
                    }
                });

            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 计算总价和数量,,,并且发送给activity进行显示
     */
    public void sendPriceAndCount() {

        double price = 0;
        int count = 0;

        for (int i = 0;i<data.size();i++){
            List<GWBean.DataBean.ListBean> listBeans = data.get(i).getList();
            for (int j = 0; j< listBeans.size(); j++){

                if (listBeans.get(j).getSelected() == 1){
                    price += listBeans.get(j).getBargainPrice() * listBeans.get(j).getNum();
                    count += listBeans.get(j).getNum();
                }
            }
        }

        //double高精度,,,计算的时候可能会出现一串数字...保留两位
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String priceString = decimalFormat.format(price);
        CountPriceBean countPriceBean = new CountPriceBean(priceString, count);

        //发送到主页面进行显示
        Message msg = Message.obtain();

        msg.what = 0;
        msg.obj = countPriceBean;
        handler.sendMessage(msg);
    }

    /**
     * 根据全选的状态更新所有商品的状态
     * @param checked
     */
    public void setAllChildsChecked(boolean checked) {

        //创建一个大的结合,,,存放所有商品的数据
        List<GWBean.DataBean.ListBean> allList = new ArrayList<>();
        for (int i= 0;i<data.size();i++){
            List<GWBean.DataBean.ListBean> listBeans = data.get(i).getList();
            for (int j=0;j<listBeans.size();j++){
                allList.add(listBeans.get(j));
            }
        }

        //显示progress
        relative_progress.setVisibility(View.VISIBLE);

        //递归更新....
        allIndex = 0;
        updateAllChecked(allList,checked);

    }
    /**
     * 更新所有的商品
     * @param allList
     * @param checked
     */
    private void updateAllChecked(final List<GWBean.DataBean.ListBean> allList, final boolean checked) {


        GWBean.DataBean.ListBean listBean = allList.get(allIndex);
        Map<String, String> params = new HashMap<>();

        //?uid=71&sellerid=1&pid=1&selected=0&num=10
        params.put("uid","3410");
        params.put("sellerid", String.valueOf(listBean.getSellerid()));
        params.put("pid", String.valueOf(listBean.getPid()));

        params.put("selected", String.valueOf(checked ? 1:0));
        params.put("num", String.valueOf(listBean.getNum()));

        OkHttp3Util.doPost(ApiUtil.updateCartUrl, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //更新一条成功
                if (response.isSuccessful()){
                    allIndex ++;

                    if (allIndex < allList.size()){
                        //继续更新下一条
                        updateAllChecked(allList,checked);
                    }else {
                        //重新查询
                        gwPersent.setUrl(ApiUtil.cartUrl);
                    }

                }
            }
        });
    }

    class MyGroupHolder{
        CheckBox checkBox;
        TextView textView;
    }

    private class MyChildHolder{
        CheckBox checkBox;
        TextView text_delete;
        ImageView imageView;
        TextView text_title;
        TextView text_price;
        TextView text_num;
        TextView text_jian;
        TextView text_add;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值