private void updateView() { // TODO Auto-generated method stub // 如果购物车中有数据,那么就显示数据,否则显示默认界面 is_choice = new boolean[arrayList_cart.size()]; tv_count.setText("(" + cartCount + ")"); if (arrayList_cart.size() != 0) { dialog.setVisibility(View.GONE); ll_cart_bottom.setVisibility(View.VISIBLE); rl_cart.setVisibility(View.VISIBLE); ll_cart.setVisibility(View.GONE); adapter = new Adapter_ListView_cart(getActivity(), arrayList_cart); adapter.setOnCheckedChanged(this); adapter.setOnAddChanged(this); adapter.setOnRedChanged(this); // adapter.setGuiChanged(this); listView_cart.setAdapter(adapter); setListViewHeightBasedOnChildren(listView_cart); } else { dialog.setVisibility(View.GONE); rl_cart.setVisibility(View.GONE); ll_cart_bottom.setVisibility(View.GONE); ll_cart.setVisibility(View.VISIBLE); } cb_cart_all.setOnCheckedChangeListener( new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { /* * 判断一:(全选按钮选中)全选按钮是否选择,如果选择,那么列表每一行都选中 * 判断二:(全选按钮取消)当取消全选按钮时,会有两种情况 * ,第一:主动点击全选按钮,此时直接取消列表所有的选中状态,第二:取消列表某一行,导致全选取消,此时列表其他行仍然是选中 * * 判断二的分析:(主动取消)判断列表每一行的选中状态,如果全部都是选中状态,那么(列表选中数=列表总数),此时属于主动取消, * 则取消所有行的选中状态,反之(被动取消)则不响应 */ // 记录列表每一行的选中状态数量 int isChoice_all = 0; System.out.println(listView_cart.getChildCount() + "数量"); if (arg1) { // 设置全选 for (int i = 0; i < arrayList_cart.size(); i++) { // 如果选中了全选,那么就将列表的每一行都选中 ((CheckBox) (listView_cart.getChildAt(i)).findViewById(R.id.cb_choice)) .setChecked(true); } } else { // 设置全部取消 for (int i = 0; i < arrayList_cart.size(); i++) { // 判断列表每一行是否处于选中状态,如果处于选中状态,则计数+1 if (((CheckBox) (listView_cart.getChildAt(i)).findViewById(R.id.cb_choice)) .isChecked()) { // 计算出列表选中状态的数量 isChoice_all += 1; } } // 判断列表选中数是否等于列表的总数,如果等于,那么就需要执行全部取消操作 if (isChoice_all == arrayList_cart.size()) { // 如果没有选中了全选,那么就将列表的每一行都不选 for (int i = 0; i < arrayList_cart.size(); i++) { // 列表每一行都取消 ((CheckBox) (listView_cart.getChildAt(i)).findViewById(R.id.cb_choice)) .setChecked(false); } } } } }); tv_cart_buy_Ordel.setOnClickListener(this); // tv_goShop.setOnClickListener(this); }
@Override public void onDataChanged(String data) { // TODO Auto-generated method stub int code = -1; JSONObject jsonObject = null; try { jsonObject = new JSONObject(data); code = jsonObject.getInt("code"); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } switch (RequestState) { case 3: try { if (code == 0) { Toast.makeText(getActivity(), "修改购物车成功", Toast.LENGTH_SHORT).show(); // Log.v("修改购物车", jsonObject.toString()); // adapter.notifyDataSetChanged(); calPriceAndWeight(tempPostionRed, 0); ((TextView) (listView_cart.getChildAt(tempPostionRed)).findViewById(R.id.tv_pop_num)) .setText( (Integer.parseInt(arrayList_cart.get(tempPostionRed).get("count").toString())) + ""); ((TextView) (listView_cart.getChildAt(tempPostionRed)).findViewById(R.id.tv_num)) .setText( "×" + (Integer.parseInt( arrayList_cart.get(tempPostionRed).get("count").toString())) + ""); } else { Toast.makeText(getActivity(), "库存不足", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } break; case 2: try { if (code == 0) { System.out.println(); Toast.makeText(getActivity(), "修改购物车成功", Toast.LENGTH_SHORT).show(); // Log.v("修改购物车", jsonObject.toString()); // adapter.notifyDataSetChanged(); calPriceAndWeight(tempPostionAdd, 1); ((TextView) (listView_cart.getChildAt(tempPostionAdd)).findViewById(R.id.tv_pop_num)) .setText( (Integer.parseInt(arrayList_cart.get(tempPostionAdd).get("count").toString())) + ""); ((TextView) (listView_cart.getChildAt(tempPostionAdd)).findViewById(R.id.tv_num)) .setText( "×" + (Integer.parseInt( arrayList_cart.get(tempPostionAdd).get("count").toString())) + ""); // adapter.onDataChanged(arrayList_cart); } else { Toast.makeText(getActivity(), "库存不足", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } break; case 4: try { if (code == 0) { JSONArray jsonArray = new JSONArray(jsonObject.getString("data")); cartCount = jsonArray.length(); if (jsonArray != null) { for (int i = 0; i < jsonArray.length(); i++) { JSONObject temp = (JSONObject) jsonArray.get(i); HashMap<String, Object> hashMap = new HashMap<String, Object>(); hashMap.put("id", temp.getInt("id")); hashMap.put("name", temp.getString("name")); hashMap.put("count", temp.getInt("count")); hashMap.put("price", temp.getString("price")); hashMap.put("ori_price", temp.getDouble("ori_price")); hashMap.put("express", temp.getString("express")); hashMap.put("sku_id", temp.getString("sku_id")); hashMap.put("psku_id", temp.getString("psku_id")); hashMap.put("sku_desc", temp.getString("sku_desc")); hashMap.put("icon_url", temp.getString("icon_url")); hashMap.put("p_id", temp.getInt("p_id")); hashMap.put("weight", temp.getString("weight")); hashMap.put("taxrate", temp.getString("taxrate")); arrayList_cart.add(hashMap); System.out.println(temp.getDouble("ori_price")); } } updateView(); // 数据请求成功时刷新页面 System.out.println(jsonObject.get("data")); } } catch (Exception e) { // TODO: handle exception updateView(); // 数据请求成功时刷新页面 e.printStackTrace(); // Toast.makeText(this, "添加购物车失败", Toast.LENGTH_SHORT).show(); } break; case 1: try { if (code == 0) { Log.v("delete", "成功"); adapter.notifyDataSetChanged(); tv_count.setText("(" + (cartCount - 1) + ")"); setListViewHeightBasedOnChildren(listView_cart); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); Toast.makeText(getActivity(), "删除失败", Toast.LENGTH_SHORT).show(); } break; default: break; } }