网络获取购物车

Activity

public class ShopcartActivity extends AppCompatActivity implements IMainActivity, View.OnClickListener {

    private CartExpanableListview expanableListview;
    private CartPresenter cartPresenter;
    private CheckBox check_all;
    private TextView text_total;
    private TextView text_buy;
    private RelativeLayout relative_progress;
    private MyAdapter myAdapter;
    private LinearLayout linear_bottom;
    private CartBean cartBean;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0){
                CountPriceBean countPriceBean = (CountPriceBean) msg.obj;
                pri = countPriceBean.getPriceString();
                text_total.setText("合计:¥"+pri);
                text_buy.setText("去结算("+countPriceBean.getCount()+")");
            }
        }
    };
    private String pri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopcart);
        //获取控件
        expanableListview = findViewById(R.id.expanable_listview);
        check_all = findViewById(R.id.check_all);
        text_total = findViewById(R.id.text_total);
        text_buy = findViewById(R.id.text_buy);
        relative_progress = findViewById(R.id.relative_progress);
        linear_bottom = findViewById(R.id.line);
        //去掉默认的指示器
        expanableListview.setGroupIndicator(null);

        cartPresenter = new CartPresenter(this);

        //1.点击全选:选中/未选中...调用适配器中的方法...myAdapter.setIsCheckAll(true);来设置所有的一级和二级是否选中,计算
        check_all.setOnClickListener(this);
        text_buy.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        relative_progress.setVisibility(View.VISIBLE);
        //请求数据
        cartPresenter.getCartData(APIUtil.cartUrl);

    }



    @Override
    public void getSuccessCartData(CartBean cartBean) {


        this.cartBean = cartBean;

        if (cartBean != null){
            //显示下面的
            linear_bottom.setVisibility(View.VISIBLE);
            relative_progress.setVisibility(View.GONE);

            //1.根据组中子条目是否选中,,,决定该组是否选中...初始化一下每一组中isGroupCheck这个数据
            for (int i = 0;i<cartBean.getData().size();i++){
                if (isAllChildInGroupSelected(i)){
                    //更改i位置 组的选中状态
                    cartBean.getData().get(i).setGroupChecked(true);
                }
            }

            //2.根据每一个组是否选中的状态,,,初始化全选是否选中
            check_all.setChecked(isAllGroupChecked());

            //设置适配器
            myAdapter = new MyAdapter(ShopcartActivity.this, cartBean,handler,cartPresenter,relative_progress);
            expanableListview.setAdapter(myAdapter);



            //展开
            for (int i= 0;i<cartBean.getData().size();i++){
                expanableListview.expandGroup(i);
            }

            //3.根据子条目是否选中  初始化价格和数量
            myAdapter.sendPriceAndCount();

        }else {
            //隐藏下面的全选.... 等
            linear_bottom.setVisibility(View.GONE);
            //显示去逛逛,,,添加购物车
            Toast.makeText(ShopcartActivity.this,"购物车为空,去逛逛",Toast.LENGTH_SHORT).show();

        }


    }

    /**
     * 所有的一级列表是否选中
     */
    private boolean isAllGroupChecked() {
        for (int i =0;i<cartBean.getData().size();i++){
            if (! cartBean.getData().get(i).isGroupChecked()){//代表一级列表有没选中的
                return false;
            }
        }

        return true;
    }

    /**
     * 判断当前组里面所有的子条目是否选中
     * @param groupPosition
     * @return
     */
    private boolean isAllChildInGroupSelected(int groupPosition) {
        for (int i= 0;i<cartBean.getData().get(groupPosition).getList().size();i++){
            //只要有一个没选中就返回false
            if (cartBean.getData().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:
                myAdapter.setAllChildState(check_all.isChecked());

                break;
            case R.id.text_buy://去结算...试一下创建订单
                CountPriceBean countPriceBean = new CountPriceBean(pri, 1);
                Intent intent=new Intent(ShopcartActivity.this,OrderActivity.class);
                intent.putExtra("money",countPriceBean);
                startActivity(intent);

                break;
        }
    }
}

Model

public class CartModel {
    private ICartPresenter iCartPresenter;

    public CartModel(ICartPresenter iCartPresenter) {
        this.iCartPresenter = iCartPresenter;
    }

    public void getCartData(final String cartUrl) {
        //获取数据
        OkHttp3Util.doGet(cartUrl, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(cartUrl,e.getLocalizedMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()){
                    final String json = response.body().string();

                    CommonUtils.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            /*if ("null".equals(json)){
                                Toast.makeText(DashApplication.getAppContext(),"购物车为空,请添加",Toast.LENGTH_SHORT).show();

                            }else {*/
                                Gson gson = new Gson();
                                CartBean cartBean = gson.fromJson(json, CartBean.class);

                                //返回数据到主线程
                                iCartPresenter.getSuccessCartJson(cartBean);
                            //}
                        }
                    });


                }
            }
        });

    }
}

presenter

public class CartPresenter implements ICartPresenter {

    private final CartModel cartModel;
    private IMainActivity iMainActivity;

    public CartPresenter(IMainActivity iMainActivity) {
        this.iMainActivity = iMainActivity;
        cartModel = new CartModel(this);
    }

    public void getCartData(String cartUrl) {
        cartModel.getCartData(cartUrl);

    }

    @Override
    public void getSuccessCartJson(CartBean cartBean) {
        //回调给view
        iMainActivity.getSuccessCartData(cartBean);
    }
}

接口

public interface ICartPresenter {
    void getSuccessCartJson(CartBean cartBean);
}

IView

public interface IMainActivity {
    void getSuccessCartData(CartBean cartBean);
}

application

public class DashApplication extends Application {

    private static Context context;
    private static Handler handler;
    private static int mainId;
    public static boolean isLoginSuccess;//是否已经登录的状态


    @Override
    public void onCreate() {
        super.onCreate();

        //关于context----http://blog.csdn.net/lmj623565791/article/details/40481055
        context = getApplicationContext();
        //初始化handler
        handler = new Handler();
        //主线程的id
        mainId = Process.myTid();


    }

    /**
     * 对外提供了context
     * @return
     */
    public static Context getAppContext() {
        return context;
    }

    /**
     * 得到全局的handler
     * @return
     */
    public static Handler getAppHanler() {
        return handler;
    }

    /**
     * 获取主线程id
     * @return
     */
    public static int getMainThreadId() {
        return mainId;
    }
}

commonutil

public class CommonUtils {
    public static final String TAG = "Dash";//sp文件的xml名称
    private static SharedPreferences sharedPreferences;

    /**
     * DashApplication.getAppContext()可以使用,但是会使用系统默认的主题样式,如果你自定义了某些样式可能不会被使用
     * @param layoutId
     * @return
     */
    public static View inflate(int layoutId) {
        View view = View.inflate(DashApplication.getAppContext(), layoutId, null);
        return view;
    }

    /**
     * dip---px
     *
     * @param dip 设备独立像素device independent px....1dp = 3px 1dp = 2px 1dp = 1.5px
     * @return
     */
    public static int dip2px(int dip) {
        //获取像素密度
        float density = DashApplication.getAppContext().getResources().getDisplayMetrics().density;
        //
        int px = (int) (dip * density + 0.5f);//100.6
        return px;

    }

    /**
     * px-dip
     *
     * @param px
     * @return
     */
    public static int px2dip(int px) {
        //获取像素密度
        float density = DashApplication.getAppContext().getResources().getDisplayMetrics().density;
        //
        int dip = (int) (px / density + 0.5f);
        return dip;

    }

    /**
     * 获取资源中的字符串
     * @param stringId
     * @return
     */
    public static String getString(int stringId) {
        return DashApplication.getAppContext().getResources().getString(stringId);
    }

    public static Drawable getDrawable(int did) {
        return DashApplication.getAppContext().getResources().getDrawable(did);
    }

    public static int getDimens(int id) {
        return DashApplication.getAppContext().getResources().getDimensionPixelSize(id);
    }

    /**
     * sp存入字符串类型的值
     * @param flag
     * @param str
     */
    public static void saveSp(String flag, String str) {
        if (sharedPreferences == null) {
            sharedPreferences = DashApplication.getAppContext().getSharedPreferences(TAG, DashApplication.getAppContext().MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(flag, str);
        edit.commit();
    }

    public static String getSp(String flag) {
        if (sharedPreferences == null) {
            sharedPreferences = DashApplication.getAppContext().getSharedPreferences(TAG, DashApplication.getAppContext().MODE_PRIVATE);
        }
        return sharedPreferences.getString(flag, "");
    }

    public static boolean getBoolean(String tag) {
        if (sharedPreferences == null) {
            sharedPreferences = DashApplication.getAppContext().getSharedPreferences(TAG, DashApplication.getAppContext().MODE_PRIVATE);
        }
        return sharedPreferences.getBoolean(tag, false);
    }

    public static void putBoolean(String tag, boolean content) {
        if (sharedPreferences == null) {
            sharedPreferences = DashApplication.getAppContext().getSharedPreferences(TAG, DashApplication.getAppContext().MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean(tag, content);
        edit.commit();
    }

    /**
     * 清除sp数据
     * @param tag
     */
    public static void clearSp(String tag) {
        if (sharedPreferences == null) {
            sharedPreferences = DashApplication.getAppContext().getSharedPreferences(TAG, DashApplication.getAppContext().MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.remove(tag);
        edit.commit();
    }

    /**
     * 自己写的运行在主线程的方法
     * 如果是主线程,执行任务,否则使用handler发送到主线程中去执行
     *
     *
     * @param runable
     */
    public static void runOnUIThread(Runnable runable) {
        //先判断当前属于子线程还是主线程
        if (android.os.Process.myTid() == DashApplication.getMainThreadId()) {
            runable.run();
        } else {
            //子线程
            DashApplication.getAppHanler().post(runable);
        }
    }
}

APIUtil

public static final  String ADD_SHOPCART="https://www.zhaoapi.cn/product/addCart";
    public static final String cartUrl = "https://www.zhaoapi.cn/product/getCarts?uid=4240";
    public static final String updateCartUrl = "https://www.zhaoapi.cn/product/updateCarts";






















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值