刷新购物车的操作以及创建订单

来源:互联网 发布:vb中四舍五入取整函数 编辑:程序博客网 时间:2024/06/05 05:51

 //在查询购物车的适配器中写访问刷新购物车的方法  并且在每次对购物车进行操作的时候 例如全选反选   一级列表改变  二级列表改变  数量改变等等 调用一下刷新购物车的方法

package com.bwei.administrator.week_3.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.TextView;import com.bumptech.glide.Glide;import com.bwei.administrator.week_3.R;import com.bwei.administrator.week_3.bean.CartBean;import com.bwei.administrator.week_3.okutil.OkHttpUtil;import java.io.IOException;import java.util.HashMap;import java.util.List;import java.util.Map;import okhttp3.Call;import okhttp3.Callback;import okhttp3.Response;/** * Created by Administrator on 2017/12/18. */public class MyExpandAdapter extends BaseExpandableListAdapter {    Context context;    List<CartBean.DataBean> groupData;    Handler handler;    public MyExpandAdapter(Context context, List<CartBean.DataBean> groupData, Handler handler) {        this.context = context;        this.groupData = groupData;        this.handler=handler;    }    @Override    public int getGroupCount() {        return groupData.size();    }    @Override    public int getChildrenCount(int groupPosition) {        return groupData.get(groupPosition).getList().size();    }    @Override    public Object getGroup(int groupPosition) {        return groupData.get(groupPosition);    }    @Override    public Object getChild(int groupPosition, int childPosition) {        return groupData.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 GroupHolder holder;        if(convertView==null){            convertView=View.inflate(context, R.layout.group_item_layout,null);            holder=new GroupHolder();            holder.check_group= (CheckBox) convertView.findViewById(R.id.check_group);            holder.text_group= (TextView) convertView.findViewById(R.id.text_group);           convertView.setTag(holder);        }else {            holder= (GroupHolder) convertView.getTag();        }        //设置值           //判断子条目是否都是1         boolean a = isAllChildSelected(groupPosition);         groupData.get(groupPosition).setGroupChecked(a);        holder.check_group.setChecked(groupData.get(groupPosition).isGroupChecked());         holder.text_group.setText(groupData.get(groupPosition).getSellerName());         //checkbox的点击事件           holder.check_group.setOnClickListener(new View.OnClickListener() {               @Override               public void onClick(View v) {                   //首先自身变为相反                   groupData.get(groupPosition).setGroupChecked(!groupData.get(groupPosition).isGroupChecked());                   //2.2根据当前一级的状态,改变该组里面二级列表的状态                   changeChildState(groupPosition,groupData.get(groupPosition).isGroupChecked());                   //2.3通过判断所有的一级组是否选中,来决定是否全选选中                   changeAllState(isAllGroupChecked());                   ///价格                   countPrice();                   notifyDataSetChanged();               }           });        return convertView;    }        /*        * 改变全选反选的状态        * */    private void changeAllState(boolean allGroupChecked) {        //将布尔值传给activity        Message msg=Message.obtain();          msg.what=1;        msg.obj=allGroupChecked;        handler.sendMessage(msg);    }    //判断是否所有组都被选中    private boolean isAllGroupChecked() {        for (int i=0;i<groupData.size();i++){            if(groupData.get(i).isGroupChecked()==false){                return false;            }        }        return true;    }    /*    * 根据一级的改变   改变他的自条目    * */    private void changeChildState(int groupPosition, boolean groupChecked) {        for (int i=0;i<groupData.get(groupPosition).getList().size();i++){            List<CartBean.DataBean.ListBean> list = groupData.get(groupPosition).getList();            //调用刷新购物车的方法            CartBean.DataBean.ListBean listBean = list.get(i);            getRefreshCartData(listBean);            list.get(i).setSelected(groupChecked?1:0);        }    }    @Override    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View view, ViewGroup parent) {        ChildHolder holder;        if (view == null){            view = View.inflate(context, R.layout.child_item_layout,null);            holder = new ChildHolder();            holder.text_add = (TextView) view.findViewById(R.id.text_add);            holder.text_num = (TextView) view.findViewById(R.id.text_num);            holder.text_jian = (TextView) view.findViewById(R.id.text_jian);            holder.text_title = (TextView) view.findViewById(R.id.text_title);            holder.text_price = (TextView) view.findViewById(R.id.text_price);            holder.image_good = (ImageView) view.findViewById(R.id.image_good);            holder.check_child = (CheckBox) view.findViewById(R.id.check_child);            view.setTag(holder);        }else {            holder = (ChildHolder) view.getTag();        }        //设置值        final List<CartBean.DataBean.ListBean> list = groupData.get(groupPosition).getList();        holder.text_num.setText(list.get(childPosition).getNum()+"");//......注意        holder.text_price.setText("¥"+list.get(childPosition).getPrice());        holder.text_title.setText(list.get(childPosition).getTitle());        //设置check的选中状态        holder.check_child.setChecked(list.get(childPosition).getSelected()==1? true:false);        Glide.with(context).load(list.get(childPosition).getImages().split("\\|")[0]).into(holder.image_good);         //子条目的点击事件           holder.check_child.setOnClickListener(new View.OnClickListener() {               @Override               public void onClick(View v) {                   //首先自身改变                    list.get(childPosition).setSelected(list.get(childPosition).getSelected()==1?0:1);                   //判断当前子条目是否选中   进行改变组的状态                   if(list.get(childPosition).getSelected()==1){                       //判断一下当前组中所有的子条目是否全部选中                       if (isAllChildSelected(groupPosition)){                           //如果全部选中改变一下当前组的状态                           changeGroupState(groupPosition,true);                           //.确定是否改变全选                           changeAllState(isAllGroupChecked());                       }                   }else {                       //如果没有选中改变一下当前组的状态                       changeGroupState(groupPosition,false);                       //.确定是否改变全选                       changeAllState(isAllGroupChecked());                   }                   CartBean.DataBean.ListBean listBean = list.get(childPosition);                   getRefreshCartData(listBean);                   //价格                   countPrice();                   //刷新                    notifyDataSetChanged();               }           });        //加号的点击事件          holder.text_add.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View v) {                  list.get(childPosition).setNum(list.get(childPosition).getNum() +1);                  //价格变化                  if(list.get(childPosition).getSelected()==1){                      CartBean.DataBean.ListBean listBean = list.get(childPosition);                      getRefreshCartData(listBean);                    countPrice();                  }                  notifyDataSetChanged();              }          });         holder.text_jian.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                if(list.get(childPosition).getNum()==1){                    return;                }                 list.get(childPosition).setNum(list.get(childPosition).getNum() -1);                 //价格变化                 if(list.get(childPosition).getSelected()==1){                     CartBean.DataBean.ListBean listBean = list.get(childPosition);                     getRefreshCartData(listBean);                     countPrice();                 }                 notifyDataSetChanged();             }         });           countPrice();        return view;    }    private void changeGroupState(int groupPosition, boolean b) {        groupData.get(groupPosition).setGroupChecked(b);    }    private boolean isAllChildSelected(int groupPosition) {        for(int i=0;i<groupData.get(groupPosition).getList().size();i++){            if(groupData.get(groupPosition).getList().get(i).getSelected()==0){                return false;            }        }        return true;    }    @Override    public boolean isChildSelectable(int groupPosition, int childPosition) {        return true;    }      /*      *      * 点击全选按钮      * */    public void checkAll(boolean checked) {        //遍历组        for(int i=0;i<groupData.size();i++){            groupData.get(i).setGroupChecked(checked);            //遍历子            List<CartBean.DataBean.ListBean> list = groupData.get(i).getList();           for (int j=0;j<list.size();j++){               list.get(j).setSelected(checked? 1:0);               CartBean.DataBean.ListBean listBean = list.get(j);               getRefreshCartData(listBean);           }        }        //计算价格        countPrice();        //刷新        notifyDataSetChanged();    }      /*      * 计算价格      * */    private void countPrice() {        double price=0;        for (int i=0;i<groupData.size();i++){            List<CartBean.DataBean.ListBean> list = groupData.get(i).getList();          for (int j=0;j<list.size();j++){              if(list.get(j).getSelected()==1){                  price+=list.get(j).getPrice()*list.get(j).getNum();              }          }        }        //将价格发送到activity        Message msg = Message.obtain();         msg.what=0;        msg.obj=price;        handler.sendMessage(msg);    }    private class GroupHolder{        CheckBox check_group;        TextView text_group;    }    private class ChildHolder{        CheckBox check_child;        ImageView image_good;        TextView text_title;        TextView text_price;        TextView text_jian;        TextView text_num;        TextView text_add;    }      //更新购物车的方法       public  void getRefreshCartData(CartBean.DataBean.ListBean listBean){           //https://www.zhaoapi.cn/product/updateCarts?uid=71&sellerid=1&pid=1&selected=0&num=10           String path ="https://www.zhaoapi.cn/product/updateCarts";           Map<String, String> parmas = new HashMap<>();            parmas.put("uid",2776+"");            parmas.put("sellerid",listBean.getSellerid()+"");            parmas.put("pid",listBean.getPid()+"");            parmas.put("selected",listBean.getSelected()+"");            parmas.put("num",listBean.getNum()+"");           OkHttpUtil.doPost(path, parmas, new Callback() {               @Override               public void onFailure(Call call, IOException e) {               }               @Override               public void onResponse(Call call, Response response) throws IOException {                   if(response.isSuccessful()){                   }               }           });       }}
   =========activity中点击结算按钮跳转创建订单的页面 将价格传过去=========

protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main2);    Intent intent = getIntent();    final String price = intent.getStringExtra("price");    //获取控件    kuan = (TextView) findViewById(R.id.text_kuan);    kuan.setText("实付款:"+price);    order = (TextView) findViewById(R.id.text_order);     order.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             OkHttpUtil.doGet("https://www.zhaoapi.cn/product/createOrder?uid=2834&price=" + price, new Callback() {                 @Override                 public void onFailure(Call call, IOException e) {                 }                 @Override                 public void onResponse(Call call, Response response) throws IOException {                     final String string = response.body().string();                     runOnUiThread(new Runnable() {                         @Override                         public void run() {                             Toast.makeText(Main2Activity.this, string,Toast.LENGTH_SHORT).show();                         }                     });                 }             });         }     });}



原创粉丝点击