购物车车 主类

来源:互联网 发布:手机淘宝兼职青青岛 编辑:程序博客网 时间:2024/04/28 07:31
public class carActivity extends AppCompatActivity implements CarKi, View.OnClickListener {    private CountPriceBean countPriceBean;    Handler handler=new Handler(){        @Override        public void handleMessage(Message msg) {            if (msg.what == 0){              countPriceBean = (CountPriceBean) msg.obj;                text_total.setText("合计:¥"+ countPriceBean.getPriceString());                text_buy.setText("去结算("+ countPriceBean.getCount()+")");            }        }    };    private CheckBox check_all;    private TextView text_total;    private TextView text_buy;    private RelativeLayout relative_progress;    private CartExpanableListview expanable_listview;    private LinearLayout linear_bottom;    private MyAdapter adapter;    private List<CartBean.DataBean> data;    private Mypresentcar presenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_car);        init();        getData();        text_buy.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent=new Intent(carActivity.this,orderActivity.class);                intent.putExtra("price",countPriceBean.getPriceString());                intent.putExtra("num",countPriceBean.getCount());                intent.putExtra("uid","3942");                startActivity(intent);                finish();            }        });    }    private void init() {        check_all =(CheckBox)findViewById(R.id.check_all);        text_total =(TextView)findViewById(R.id.text_total);        text_buy =(TextView)findViewById(R.id.text_buy);        relative_progress =(RelativeLayout)findViewById(R.id.relative_progress);        linear_bottom =(LinearLayout)findViewById(R.id.linear_layout);        expanable_listview = (CartExpanableListview)findViewById(R.id.expanable_listview);        expanable_listview.setGroupIndicator(null);//去掉默认的指示器        check_all.setOnClickListener(this);    }    private void getData() {         presenter =new Mypresentcar(this);        presenter.getData("https://www.zhaoapi.cn/product/getCarts","3942");    }    @Override    public void getDataForMat(final String json) {        runOnUiThread(new Runnable() {          @Override            public void run() {                if(json!=null){                    Gson gson=new Gson();                    CartBean cartBean = gson.fromJson(json, CartBean.class);                    data = cartBean.getData();                    linear_bottom.setVisibility(View.VISIBLE);                    relative_progress.setVisibility(View.GONE);                    //1.根据组中子条目是否选中,,,决定该组是否选中...初始化一下每一组中isGroupCheck这个数据                    for (int i = 0;i<data.size();i++){                        if (isAllChildInGroupSelected(i)){                            //更改i位置 组的选中状态                            data.get(i).setGroupChecked(true);                        }                    }                    //2.根据每一个组是否选中的状态,,,初始化全选是否选中                    check_all.setChecked(isAllGroupChecked());                    //设置适配器                    adapter =new MyAdapter(carActivity.this,cartBean,handler,presenter,relative_progress);                    expanable_listview.setAdapter(adapter);                    //展开列表                    for(int i = 0; i< cartBean.getData().size(); i++){                        expanable_listview.expandGroup(i);                    }                    //3.根据子条目是否选中  初始化价格和数量                    adapter.sendPriceAndCount();                }else{                    //隐藏下面的全选.... 等                    linear_bottom.setVisibility(View.GONE);                    //显示去逛逛,,,添加购物车                    Toast.makeText(carActivity.this,"购物车为空,去逛逛",Toast.LENGTH_SHORT).show();                }            }        });    }/**     * 所有的一级列表是否选中     */    private boolean isAllGroupChecked() {        for (int i =0;i<data.size();i++){            if (! data.get(i).isGroupChecked()){//代表一级列表有没选中的                return false;            }        }        return true;    }    /**     * 判断当前组里面所有的子条目是否选中     * @param groupPosition     * @return     */    private boolean isAllChildInGroupSelected(int groupPosition) {        for (int i= 0;i<data.get(groupPosition).getList().size();i++){            //只要有一个没选中就返回false            if (data.get(groupPosition).getList().get(i).getSelected() ==0){                return false;            }        }        return true;    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.check_all:                adapter.setAllChildState(check_all.isChecked());                   break;        }    }}
原创粉丝点击