/** * 获取所有选中的商品 * * @return 商品数据 */ public static List<ModelLocalCar> getSelectedLocalCars() { // 创建集合用于存放选中的商品 List<ModelLocalCar> selectedLocalCars = new ArrayList<ModelLocalCar>(); try { // 获取所有购物车商品 JSONArray array = new JSONArray(Content.getStringContent(Parameters.CACHE_KEY_CAR_LOCAL, "[]")); // 遍历所有店铺 for (int i = 0; i < array.length(); i++) { // 创建单个店铺对象 ModelLocalCar localCar = new ModelLocalCar(array.optJSONObject(i)); // 单个店铺json数据 JSONObject selectedObject = localCar.toJsonObject(); if (selectedObject.has("goods")) { // 先去除店铺下所有的商品数据 selectedObject.remove("goods"); } // 用于存放选中的商品 JSONArray selectedGoodsArray = new JSONArray(); // 遍历店铺内添加的所有产品 for (int j = 0; j < localCar.getCarGoods().size(); j++) { if (localCar.getCarGoods().get(j).isSelected()) { // 商品选中,则添加到新集合 selectedGoodsArray.put(localCar.getCarGoods().get(j).toJsonObject()); } } selectedObject.put("goods", selectedGoodsArray); ModelLocalCar selectedLocalCar = new ModelLocalCar(selectedObject); if (!selectedLocalCar.getCarGoods().isEmpty()) { selectedLocalCars.add(selectedLocalCar); } } } catch (JSONException e) { e.printStackTrace(); } return selectedLocalCars; }