购物车--ExpandableListView实现

布局




    
    
    

        

        

        

        

java代码

package com.bwei.gouwuchemoni;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    String url = "https://www.zhaoapi.cn/product/getCarts?uid=71";
    private ExpandableListView elc_show_main;
    private CheckBox cb_allCheck_main;
    private TextView btn_allPrice_main;
    private Button btn_allNumber_main;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        //得到okhttp
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        Request request = new Request.Builder().url(url).build();
        okHttpClient.newCall(request).enqueue(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() {
                        Gson gson = new Gson();
                        CartInfo cartInfo = gson.fromJson(string, CartInfo.class);
                        List sellerData = cartInfo.getData();
                        //创建适配
                        myAdapter = new MyAdapter(sellerData);
                        //调用适配器的借口 来实现回电数据
                        myAdapter.setOnCartListChangeListener(new MyAdapter.OnCartListChangeListener() {

                            @Override
                            public void SellerSelectedChange(int groupPosition) {
                                //先得到 checkbox
                                boolean b = myAdapter.isCurrentSellerAllProductSelected(groupPosition);
                                //改变所有当前商家的选中状态
                                myAdapter.changeCurrentSellerAllProductSelected(groupPosition,!b);
                                myAdapter.notifyDataSetChanged();
                                refreshAllSelectedAndTotalPriceAndTotalNumber();

                            }

                            @Override
                            public void changeCurrentProductSelected(int groupPosition, int childPosition) {
                                myAdapter.changeCurrentProductSelected(groupPosition,childPosition);
                                myAdapter.notifyDataSetChanged();
                                refreshAllSelectedAndTotalPriceAndTotalNumber();
                            }

                            @Override
                            public void ProductNumberChange(int groupPosition, int childPosition, int number) {
                                myAdapter.changeCurrentProductNumber(groupPosition,childPosition,number);
                                myAdapter.notifyDataSetChanged();
                                //刷新底部的方法
                                refreshAllSelectedAndTotalPriceAndTotalNumber();
                            }
                        });

                        elc_show_main.setAdapter(myAdapter);
//
                        for (int i = 0; i 

 

你可能感兴趣的:(购物车--ExpandableListView实现)