MainActivity页面

博客展示了一段Java代码,定义了一个名为MainActivity的类,该类继承自AppCompatActivity,并实现了Contract.Iview接口和View.OnClickListener接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class MainActivity extends AppCompatActivity implements Contract.Iview, View.OnClickListener {

private TextView title;
private RecyclerView recycler_view;
private Presenter presenter;
private CheckBox box_all;
private TextView text_price;
private Button btn_count;
private ShowAdapter showAdapter;
private List<DataBean> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //初始化控件
    initView();
    //调用p层
    presenter = new Presenter();
    presenter.attch(this);
    presenter.showUrl(Api_hou.URL);
    box_all.setOnClickListener(this);
    title.setText("购物车");
}

private void initView() {
    title = findViewById(R.id.text_title);
    recycler_view = findViewById(R.id.recycler_view);
    box_all = findViewById(R.id.box_all);
    text_price = findViewById(R.id.text_price);
    btn_count = findViewById(R.id.btn_count);
}

@Override
public void getRequest(Object data) {
    Gson gson = new Gson();
    ShowBean showBean = gson.fromJson(String.valueOf(data), ShowBean.class);
    List<DataBean> dataBeans = showBean.getData();
    showAdapter = new ShowAdapter(dataBeans, MainActivity.this);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recycler_view.setLayoutManager(linearLayoutManager);
    recycler_view.setAdapter(showAdapter);
    showAdapter.setLisenter(new ShowAdapter.ShopShowCallBack() {
        @Override
        public void callBack(List<DataBean> list) {
            double totalPrice = 0;
            int num = 0;
            int totalNum = 0;
            for (int i = 0; i < list.size(); i++) {
                List<ListBean> listtotal = list.get(i).getList();
                for (int a = 0; a < listtotal.size(); a++) {
                    totalNum = totalNum - listtotal.get(a).getNum();
                    ////取选中的状态
                    if (listtotal.get(a).isCheck()) {
                        totalPrice = totalPrice + (listtotal.get(a).getPrice() * listtotal.get(a).getNum());
                        num = num +listtotal.get(a).getNum();
                    }
                }
            }
            if (num < totalNum) {
                box_all.setChecked(false);
            } else {
                box_all.setChecked(true);
            }
            text_price.setText("合计:" + totalPrice);
            btn_count.setText("去结算(" + num + ")");
        }
    });
    if (data instanceof ShowBean){
        ShowBean showBean1= (ShowBean) data;
        list = showBean1.getData();
        if (list !=null){
            list.remove(0);
            showAdapter.setList(list);
        }
    }
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.box_all:
            checkedSell(box_all.isChecked());
            showAdapter.notifyDataSetChanged();
            break;
    }
}
private void checkedSell(boolean checked) {
    double priceAll=0;
    int numAll=0;
    for (int i = 0; i <list.size(); i++) {
        DataBean dataBean = list.get(i);
        dataBean.setChecked(checked);
        List<ListBean> listAll =list.get(i).getList();
        for (int a=0;a<listAll.size();a++){
            listAll.get(a).setCheck(checked);
            priceAll=priceAll+(listAll.get(a).getPrice()*listAll.get(a).getNum());
            numAll=numAll+listAll.get(a).getNum();
        }
    }
    if (checked){
        text_price.setText("合计:"+priceAll);
        btn_count.setText("去结算("+numAll+")");
    }else{
        text_price.setText("合计:0.00");
        btn_count.setText("去结算(0)");
    }
}

//修改选中状态,获取价格和数量
@Override
protected void onDestroy() {
    super.onDestroy();
    presenter.detch();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值