购物车适配器

来源:互联网 发布:java ssh实战项目 编辑:程序博客网 时间:2024/06/07 12:32
package com.example.lianxi.Adapter;import android.content.Context;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.CheckBox;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;import com.example.lianxi.Bena.CartBean;import com.example.lianxi.Bena.CountPriceBean;import com.example.lianxi.Presenter.Mypresentcar;import com.example.lianxi.R;import com.example.lianxi.Utils.ImaegLoader;import com.example.lianxi.Utils.OkHttp;import com.nostra13.universalimageloader.core.ImageLoader;import java.io.IOException;import java.text.DecimalFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import okhttp3.Call;import okhttp3.Callback;import okhttp3.Response;import static com.example.lianxi.Utils.OkHttp.doPost;public class MyAdapter extends BaseExpandableListAdapter{    private RelativeLayout relative_progress;    private Handler handler;    private Context context;    private CartBean cartBean;    private Mypresentcar presenter;    private int size;    private int childI;    private int allSize;    private int index;    public MyAdapter(Context context, CartBean cartBean, Handler handler, Mypresentcar presenter,RelativeLayout relative_progress ) {        this.context = context;        this.cartBean = cartBean;       this.presenter=presenter;        this.handler=handler;        this.relative_progress=relative_progress;    }    @Override    public int getGroupCount() {        return cartBean.getData().size();    }    @Override    public int getChildrenCount(int groupPosition) {        return cartBean.getData().get(groupPosition).getList().size();    }    @Override    public Object getGroup(int groupPosition) {        return cartBean.getData().get(groupPosition);    }    @Override    public Object getChild(int groupPosition, int childPosition) {        return cartBean.getData().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(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {        final groupHolder vh;        if(convertView==null){            vh=new groupHolder();            convertView = convertView.inflate(context, R.layout.group_item_layout, null);            vh.check_group=(CheckBox)convertView.findViewById(R.id.check_group);            vh.text_group=(TextView)convertView.findViewById(R.id.text_group);            convertView.setTag(vh);        }else{            vh=(groupHolder)convertView.getTag();        }       final CartBean.DataBean dataBean = cartBean.getData().get(groupPosition);        vh.check_group.setChecked(dataBean.isGroupChecked());        vh.text_group.setText(dataBean.getSellerName());        //组的点击事件...也要去请求更新的接口        vh.check_group.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                relative_progress.setVisibility(View.VISIBLE);//显示                size = dataBean.getList().size();                childI = 0;                updateAllInGroup(vh.check_group.isChecked(),dataBean);            }        });        return convertView;    }    private void updateAllInGroup(final boolean checked, final CartBean.DataBean dataBean) {        CartBean.DataBean.ListBean listBean = dataBean.getList().get(childI);//0        Map<String, String> params = new HashMap<>();        params.put("uid","3942");        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()));        doPost("https://www.zhaoapi.cn/product/updateCarts", params, new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                if (response.isSuccessful()){                    childI = childI+1;//0,1,2...3                    if (childI <size){                        updateAllInGroup(checked,dataBean);                    }else {                        //所有的条目已经更新完成....再次请求查询购物车的数据                         presenter.getData("https://www.zhaoapi.cn/product/getCarts","3942");                    }                }            }        });    }    @Override    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {        childHolder vh;        if(convertView==null){            convertView = convertView.inflate(context, R.layout.chid_item_layout, null);            vh=new childHolder();            vh.check_child=(CheckBox)convertView.findViewById(R.id.check_child);            vh.image_good=(ImageView)convertView.findViewById(R.id.image_good);            vh.text_title=(TextView)convertView.findViewById(R.id.text_title);            vh.text_price=(TextView)convertView.findViewById(R.id.text_price);            vh.text_jian=(TextView)convertView.findViewById(R.id.text_jian);            vh.text_num=(TextView)convertView.findViewById(R.id.text_num);            vh.text_add=(TextView)convertView.findViewById(R.id.text_add);            vh.text_delete=(TextView)convertView.findViewById(R.id.text_delete);            convertView.setTag(vh);        }else{            vh=(childHolder)convertView.getTag();        }        final CartBean.DataBean.ListBean listBean = cartBean.getData().get(groupPosition).getList().get(childPosition);        vh.text_num.setText(listBean.getNum()+"");        vh.text_price.setText("¥"+listBean.getBargainPrice());        vh.check_child.setChecked(listBean.getSelected()==0?false:true);        vh.text_title.setText(listBean.getTitle());        ImageLoader.getInstance().displayImage(listBean.getImages(),vh.image_good, ImaegLoader.getDefultOption());        //点击事件        vh.check_child.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                updateChildChecked(listBean);            }        });       //加号        vh.text_add.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //请求更新的接口                updateChildNum(listBean,true);            }        });        //减号        vh.text_jian.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (listBean.getNum() == 1){                    return;                }                //更新数量,,,减                updateChildNum(listBean,false);            }        });        //删除        vh.text_delete.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(listBean.getSelected()==1){                    relative_progress.setVisibility(View.VISIBLE);                    deletechilditem(listBean);                }else{                    Toast.makeText(context,"请选择",Toast.LENGTH_SHORT).show();                }            }        });        return  convertView;    }   //删除    private void deletechilditem(CartBean.DataBean.ListBean listBean) {        Map<String, String> params=new HashMap<>();        params.put("uid","3942");        params.put("pid", String.valueOf(listBean.getPid()));        OkHttp.doPost("https://www.zhaoapi.cn/product/deleteCart", params, new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                   if(response.isSuccessful()){                       presenter.getData("https://www.zhaoapi.cn/product/getCarts","3942");                   }            }        });    }    //更新数量    private void updateChildNum(CartBean.DataBean.ListBean listBean, boolean b) {        //一旦执行更新的操作,,,progressBar显示        relative_progress.setVisibility(View.VISIBLE);        //?uid=71&sellerid=1&pid=1&selected=0&num=10        Map<String, String> params = new HashMap<>();        params.put("uid","3942");        params.put("sellerid", String.valueOf(listBean.getSellerid()));        params.put("pid", String.valueOf(listBean.getPid()));        params.put("selected", String.valueOf(listBean.getSelected()));        if (b){            params.put("num", String.valueOf(listBean.getNum() + 1));        }else {            params.put("num", String.valueOf(listBean.getNum() - 1));        }        OkHttp.doPost("https://www.zhaoapi.cn/product/updateCarts", params, new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                //更新成功之后...网络上的数据发生了改变...再次请求购物车的接口进行数据的展示                if (response.isSuccessful()){                    presenter.getData("https://www.zhaoapi.cn/product/getCarts","3942");                }            }        });    }    private void updateChildChecked(CartBean.DataBean.ListBean listBean) {//一旦执行更新的操作,,,progressBar显示        relative_progress.setVisibility(View.VISIBLE);        //?uid=71&sellerid=1&pid=1&selected=0&num=10        Map<String, String> params = new HashMap<>();        params.put("uid","3942");        params.put("sellerid", String.valueOf(listBean.getSellerid()));        params.put("pid", String.valueOf(listBean.getPid()));        params.put("selected", String.valueOf(listBean.getSelected() == 0? 1:0));        params.put("num", String.valueOf(listBean.getNum()));       OkHttp.doPost("https://www.zhaoapi.cn/product/updateCarts", params, new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                //更新成功之后...网络上的数据发生了改变...再次请求购物车的接口进行数据的展示                if (response.isSuccessful()){                    presenter.getData("https://www.zhaoapi.cn/product/getCarts","3942");                }            }        });    }    public void sendPriceAndCount() {        double price = 0;        int count = 0;        //通过判断二级列表是否勾选,,,,计算价格数量        for (int i=0;i<cartBean.getData().size();i++){            for (int j = 0;j<cartBean.getData().get(i).getList().size();j++){                if (cartBean.getData().get(i).getList().get(j).getSelected() == 1){                    //价格是打折的价格...........                    price += cartBean.getData().get(i).getList().get(j).getNum() * cartBean.getData().get(i).getList().get(j).getBargainPrice();                    count += cartBean.getData().get(i).getList().get(j).getNum();                }            }        }        //精准的保留double的两位小数        DecimalFormat decimalFormat = new DecimalFormat("#.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);    }    @Override    public boolean isChildSelectable(int groupPosition, int childPosition) {        return true;    }    public void setAllChildState(boolean checked) {        //创建一个集合 装所有的子条目        List<CartBean.DataBean.ListBean> allList = new ArrayList<>();        for (int i=0;i<cartBean.getData().size();i++){            for (int j=0;j<cartBean.getData().get(i).getList().size();j++){                allList.add(cartBean.getData().get(i).getList().get(j));            }        }        relative_progress.setVisibility(View.VISIBLE);        allSize = allList.size();        index = 0;        //通过 递归 更新所有子条目的选中        updateAllChild(allList,checked);    }    private void updateAllChild(final List<CartBean.DataBean.ListBean> allList, final boolean checked) {        CartBean.DataBean.ListBean listBean = allList.get(index);//0        //跟新的操作        Map<String, String> params = new HashMap<>();        params.put("uid","3942");        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()));        OkHttp.doPost("https://www.zhaoapi.cn/product/updateCarts", params, new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                if (response.isSuccessful()){                    index = index +1;//0,1,2......3                    if (index < allSize){                        updateAllChild(allList,checked);                    }else {                        //查询购物车                        presenter.getData("https://www.zhaoapi.cn/product/getCarts","3942");                    }                }            }        });    }    class groupHolder{        CheckBox check_group;        TextView text_group;    }    class childHolder{        CheckBox check_child;        ImageView image_good;        TextView text_title;        TextView text_price;        TextView text_jian;        TextView text_num;        TextView text_add;        TextView text_delete;    }}