android 购物车 listview嵌套问题
16lz
2021-01-24
该购物车使用3个listview嵌套完成,具有3级分类:仓库、商铺、商家,效果图如下:
扫描关注我的微信公众号:
代码直接贴出来:
package com.globex.o2o.controllers.shoppingCart;import android.content.DialogInterface;import android.content.Intent;import android.graphics.Color;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v7.app.AlertDialog;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.CheckBox;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.ProgressBar;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;import com.globex.core.android.utils.ViewUtils;import com.globex.core.common.utils.JsonUtils;import com.globex.o2o.R;import com.globex.o2o.Utils.CommonUtils;import com.globex.o2o.Utils.ResponseUtils;import com.globex.o2o.constants.Api;import com.globex.o2o.controllers.BaseFragment;import com.globex.o2o.controllers.order.VerifyOrder;import com.globex.o2o.http.DefaultStringCallBack;import com.google.gson.Gson;import com.zhy.http.okhttp.OkHttpUtils;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import org.xutils.x;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import de.greenrobot.event.EventBus;import okhttp3.Call;public class ShoppingCartFragment extends BaseFragment { private ListView mListView;// 列表 private View rootView; private ProgressBar pb; private boolean isEdit = false; Gson gson = new Gson(); /** * 解析数据 */ private List wareHouseList = new ArrayList<>(); private List selectGoods = new ArrayList<>(); @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (rootView == null) { rootView = inflater.inflate(R.layout.fragment_shoppingcar_layout, null); } initView(); initListener(); return rootView; } private float selectTotalPrice, selectTotalTax; //EventBus处理函数 public void onEvent(EBMassage msg) { ArrayList goodList = msg.getGoodList(); int what = msg.getWhat(); //商品添加 for (int i = 0; i < goodList.size(); i++) { Goods goods = goodList.get(i); if (what == 1) { if (selectGoods.contains(goods)) { selectGoods.remove(goods); } selectGoods.add(goods); } if (what == 2) { selectGoods.remove(goods); } } reflashBalance(); reflash(); } private SwipeRefreshLayout swipeRefreshLayout; private TextView shoppingcar_subtitle, shoppingcar_txtv_submit, shoppingcar_txtv_tollPrice, shoppingcar_txtv_tax; private CheckBox shoppingcar_check_box_all; private RelativeLayout empty_view; //初始化视图 public void initView() { pb = (ProgressBar) rootView.findViewById(R.id.shoppingcar_pb); empty_view = (RelativeLayout) rootView.findViewById(R.id.shoppingcar_rl_empty); //下拉刷新 swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.shoppingCar_swipeRefreshLayout); swipeRefreshLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.CYAN); mListView = (ListView) rootView.findViewById(R.id.listview); firstAdapter = new FirstListViewAdapter(); mListView.setAdapter(firstAdapter); adapterList.add(firstAdapter); shoppingcar_subtitle = (TextView) rootView.findViewById(R.id.shoppingcar_subtitle); shoppingcar_check_box_all = (CheckBox) rootView.findViewById(R.id.shoppingcar_check_box_all); shoppingcar_txtv_submit = (TextView) rootView.findViewById(R.id.shoppingcar_txtv_submit); shoppingcar_txtv_tollPrice = (TextView) rootView.findViewById(R.id.shoppingcar_txtv_tollPrice); shoppingcar_txtv_tax = (TextView) rootView.findViewById(R.id.shoppingcar_txtv_tax); } //初始化监听器 public void initListener() { swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { loadData(); } }); //全选Checkbox shoppingcar_check_box_all.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //全选 selectAllB(); } }); //右上角的文字 shoppingcar_subtitle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!empty_view.isShown()) { shoppingcar_subtitle.setTextColor(Color.WHITE); } else { shoppingcar_subtitle.setText("编辑"); shoppingcar_subtitle.setTextColor(Color.GRAY); return; } if (shoppingcar_subtitle.getText().equals("编辑")) { shoppingcar_subtitle.setText("完成"); isEdit = true; } else { shoppingcar_subtitle.setText("编辑"); isEdit = false; loadData(); } reflash(); } }); //提交 shoppingcar_txtv_submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { submit(); } }); } //加载数 private void loadData() { if (!CommonUtils.getHttpHeadParams(getActivity()).containsKey("tk")) { return; } pb.setVisibility(View.VISIBLE); wareHouseList.clear(); cSellerList.clear(); cGoodsList.clear(); OkHttpUtils .get() .headers(CommonUtils.getHttpHeadParams(getApplication())) .url(getUrl(Api.getShoppingCar)) .build() .execute(new DefaultStringCallBack() { @Override public void onResponse(String response) { pb.setVisibility(View.GONE); swipeRefreshLayout.setRefreshing(false); try { JSONObject object = new JSONObject(response); if (object.getInt("code") == 200) { parseJson(object); reflash(); } else { ResponseUtils.checkTokenOfShoppingCar(getActivity(), object.getInt("code")); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(Call call, Exception e) { pb.setVisibility(View.GONE); swipeRefreshLayout.setRefreshing(false); Toast.makeText(getApplication(), "获取购物车失败,错误信息:" + e.getMessage(), Toast.LENGTH_SHORT).show(); } }); } //刷新结算信息 private void reflashBalance() { selectTotalPrice = 0; selectTotalTax = 0; for (int i = 0; i < selectGoods.size(); i++) { selectTotalPrice += selectGoods.get(i).getTotal_price(); selectTotalTax += selectGoods.get(i).getTotal_tax(); } shoppingcar_txtv_submit.setText("结算(" + selectGoods.size() + ")"); shoppingcar_txtv_tollPrice.setText("¥" + selectTotalPrice); shoppingcar_txtv_tax.setText("(含关税:¥" + selectTotalTax + ",不含运费)"); } private FirstListViewAdapter firstAdapter; private List> cSellerList = new ArrayList<>(); private List> cGoodsList = new ArrayList<>(); //保存选中的状态 private List>> itemIsChecked = new ArrayList<>(); private List> subIsChecked = new ArrayList<>(); private HashMap listState = new HashMap<>(); private String info; private boolean isEdited = false; /** * 解析数据 * * @throws JSONException */ private void parseJson(JSONObject obj) throws JSONException { isEdit = false; isEdited = false; shoppingcar_subtitle.setText("编辑"); cGoodsList.clear(); cSellerList.clear(); wareHouseList.clear(); JSONObject data = obj.getJSONObject("data"); info = obj.getString("info"); if (data.length() == 0) { shoppingcar_subtitle.setTextColor(Color.GRAY); shoppingcar_subtitle.setText("编辑"); empty_view.setVisibility(View.VISIBLE); ((TextView) rootView.findViewById(R.id.shoppingcar_txtv_noids)).setText(obj.getString("info")); ((RelativeLayout) rootView.findViewById(R.id.bottom_bar)).setVisibility(View.GONE); return; } else { shoppingcar_subtitle.setTextColor(Color.WHITE); empty_view.setVisibility(View.GONE); ((RelativeLayout) rootView.findViewById(R.id.bottom_bar)).setVisibility(View.VISIBLE); } JSONArray arr = data.getJSONArray("cart_list"); for (int i = 0; i < arr.length(); i++) { JSONObject obj1 = arr.getJSONObject(i); wareHouseList.add(obj1.getString("warehouse")); JSONArray content = obj1.getJSONArray("content"); List sellerList = new ArrayList<>(); List> goodsList = new ArrayList<>(); HashMap> isCd = new HashMap<>(); ArrayList checkBoxes = new ArrayList<>(); for (int j = 0; j < content.length(); j++) { JSONObject obj2 = content.getJSONObject(j); JSONObject seller = obj2.getJSONObject("seller"); Seller seller1 = gson.fromJson(seller.toString(), Seller.class); sellerList.add(seller1); JSONArray goodsarr = obj2.getJSONArray("goods"); ArrayList goodses = new ArrayList<>(); HashMap isC = new HashMap<>(); for (int k = 0; k < goodsarr.length(); k++) { JSONObject goods = goodsarr.getJSONObject(k); Goods goods1 = gson.fromJson(goods.toString(), Goods.class); goodses.add(goods1); isC.put(goods1.getCart_id(), false); } checkBoxes.add(false); isCd.put(seller1.getName(), isC); goodsList.add(goodses); } if (!listState.containsKey(obj1.getString("warehouse"))) { listState.put(obj1.getString("warehouse"), false); } subIsChecked.add(checkBoxes); itemIsChecked.add(isCd); cGoodsList.add(goodsList); cSellerList.add(sellerList); } } //第一个Adapter class FirstListViewAdapter extends BaseAdapter { @Override public int getCount() { return wareHouseList.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final List> cl = cGoodsList.get(position); final FirstListViewHolder holder; final HashMap> arrayLists = itemIsChecked.get(position); final ArrayList state = subIsChecked.get(position); if (convertView == null) { holder = new FirstListViewHolder(); convertView = LayoutInflater.from(getActivity()).inflate(R.layout.shoppingcarfragment_listviewitem1, null); holder.lv = (ListView) convertView.findViewById(R.id.shopping_subListView); holder.ck = (CheckBox) convertView.findViewById(R.id.shoppingcar_sub_checbox); holder.warehouse = (TextView) convertView.findViewById(R.id.listViewItem1_warehouse); SubAdapter subAdapter = new SubAdapter(cSellerList.get(position), cl, arrayLists, state, position); adapterList.add(subAdapter); holder.lv.setAdapter(subAdapter); ViewUtils.setListViewHeightBasedOnChildren(holder.lv); convertView.setTag(holder); } else { holder = (FirstListViewHolder) convertView.getTag(); } holder.ck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean checked = holder.ck.isChecked(); listState.put(wareHouseList.get(position), checked); //通知子改变状态 for (int i = 0; i < state.size(); i++) { state.set(i, checked); } //通知孙改变状态 Iterator>> iterator1 = arrayLists.entrySet().iterator(); while (iterator1.hasNext()) {// for (int i = 0; i < arrayLists.size(); i++) { HashMap list = iterator1.next().getValue();// for (int j = 0; j < list.size(); j++) {// list.set(j, checked);// } Iterator> iterator = list.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry next = iterator.next(); next.setValue(checked); } } //改变全选的状态 if (listState.containsValue(false)) { shoppingcar_check_box_all.setChecked(false); } else { shoppingcar_check_box_all.setChecked(true); } //刷新数据 reflash(); //勾选商品变化 for (int i = 0; i < cl.size(); i++) { ArrayList goodses = cl.get(i); for (int j = 0; j < goodses.size(); j++) { if (checked) { EventBus.getDefault().post(new EBMassage(goodses, 1)); } else { EventBus.getDefault().post(new EBMassage(goodses, 2)); } } } } }); holder.warehouse.setText(wareHouseList.get(position)); holder.ck.setChecked(listState.get(wareHouseList.get(position))); return convertView; } } private List subStatus; //第二个Adapter class SubAdapter extends BaseAdapter { private List datas; private List> clist; private HashMap> misC; int p; @Override public int getCount() { return datas.size(); } public SubAdapter(List data, List> cl, HashMap> isC, ArrayList state, int p0) { datas = data; p = p0; clist = cl; misC = isC; subStatus = state; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { if (clist.get(position).size() == 0) { View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.sublistview, null); inflate.findViewById(R.id.sub_root_view).setVisibility(View.GONE); return inflate; } final SubListViewHolder holder; SubListView_ListViewAdapter subSubAdapter = null; if (convertView == null) { holder = new SubListViewHolder(); convertView = LayoutInflater.from(getActivity()).inflate(R.layout.sublistview, null); holder.ck = (CheckBox) convertView.findViewById(R.id.sublistView_checbox); holder.lv = (ListView) convertView.findViewById(R.id.sublistView_ListView); holder.tvtv = (TextView) convertView.findViewById(R.id.subListview_sellerName); convertView.setTag(holder); if (misC.get(datas.get(position).getName()) == null) { misC.put(datas.get(position).getName(), new HashMap(100)); subStatus.add(false); } subSubAdapter = new SubListView_ListViewAdapter(clist.get(position), misC.get(datas.get(position).getName()), position, p); adapterList.add(subSubAdapter); holder.lv.setAdapter(subSubAdapter); ViewUtils.setListViewHeightBasedOnChildren(holder.lv); } else { holder = (SubListViewHolder) convertView.getTag(); } holder.tvtv.setText(datas.get(position).getName()); holder.ck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean checked = holder.ck.isChecked(); subStatus.set(position, checked); //使子checkbox也变状态// for (int j = 0; j < isC.size(); j++) {// isC.set(j, checked);// } Iterator> iterator = misC.get(datas.get(position).getName()).entrySet().iterator(); while (iterator.hasNext()) { Map.Entry next = iterator.next(); next.setValue(checked); } //勾选商品变化 if (checked) { EventBus.getDefault().post(new EBMassage(clist.get(position), 1)); } else { EventBus.getDefault().post(new EBMassage(clist.get(position), 2)); } //设置父类的状态 if (subStatus.contains(false)) { listState.put(wareHouseList.get(p), false); } else { listState.put(wareHouseList.get(p), true); } //设置全选按钮的状态 if (listState.containsValue(false)) { shoppingcar_check_box_all.setChecked(false); } else { shoppingcar_check_box_all.setChecked(true); } //刷新数据 reflash(); } }); holder.ck.setChecked(subStatus.get(position)); return convertView; } } //第三个Adapter class SubListView_ListViewAdapter extends BaseAdapter { ArrayList datas; HashMap mIsChecked; int p; int ye; @Override public int getCount() { return datas.size(); } public SubListView_ListViewAdapter(ArrayList data, HashMap isC, int po, int y) { datas = data; mIsChecked = isC; p = po; ye = y; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final Goods goods = datas.get(position); final ThreeListViewHolder holder; if (null == convertView) { holder = new ThreeListViewHolder(); convertView = LayoutInflater.from(getActivity()).inflate(R.layout.cart_list_item, null); holder.ck = (CheckBox) convertView.findViewById(R.id.car_item_check_box); holder.count = (TextView) convertView.findViewById(R.id.car_item_numbTxtv); holder.imgv = (ImageView) convertView.findViewById(R.id.car_item_imgv); holder.intro = (TextView) convertView.findViewById(R.id.car_item_intro); holder.price = (TextView) convertView.findViewById(R.id.car_item_priceTxtv); holder.tax = (TextView) convertView.findViewById(R.id.car_item_taxTxtv); holder.shoppingcar_ll_tianshan = (LinearLayout) convertView.findViewById(R.id.shoppingcar_ll_tianshan); holder.shoppingcar_txtv_jian = (TextView) convertView.findViewById(R.id.shoppingcar_txtv_jian); holder.shoppingcar_txtv_add = (TextView) convertView.findViewById(R.id.shoppingcar_txtv_add); holder.shoppingcar_edit = (TextView) convertView.findViewById(R.id.shoppingcar_edit); convertView.setTag(holder); } else { holder = (ThreeListViewHolder) convertView.getTag(); } if (isEdit) { holder.ck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //删除商品 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("确认删除?").setNegativeButton("删除", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { delete(goods, datas, position); } }).setPositiveButton("否", null).show(); } }); holder.intro.setVisibility(View.GONE); holder.tax.setVisibility(View.GONE); holder.ck.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_delete)); holder.shoppingcar_ll_tianshan.setVisibility(View.VISIBLE); //添加数量 holder.shoppingcar_txtv_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { holder.shoppingcar_edit.setText((Integer.parseInt(holder.shoppingcar_edit.getText().toString()) + 1) + ""); editNumb(goods, Integer.parseInt(holder.shoppingcar_edit.getText().toString())); } }); //减少数量 holder.shoppingcar_txtv_jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int numb = Integer.parseInt(holder.shoppingcar_edit.getText().toString()); if (numb == 1) { return; } else { holder.shoppingcar_edit.setText((numb - 1) + ""); } editNumb(goods, Integer.parseInt(holder.shoppingcar_edit.getText().toString())); } }); } else { holder.ck.setButtonDrawable(getResources().getDrawable(R.drawable.check_box_bg)); holder.shoppingcar_ll_tianshan.setVisibility(View.GONE); holder.intro.setVisibility(View.VISIBLE); holder.tax.setVisibility(View.VISIBLE); holder.intro.setText(goods.getName()); holder.tax.setText("适用税率:" + goods.getTotal_tax()); if (mIsChecked.get(goods.getCart_id()) != null) { holder.ck.setChecked((mIsChecked.get(goods.getCart_id()))); } else { mIsChecked.put(goods.getCart_id(), false); } holder.ck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ArrayList arrayList = new ArrayList<>(); arrayList.add(datas.get(position)); if (holder.ck.isChecked()) { EventBus.getDefault().post(new EBMassage(arrayList, 1)); } else { EventBus.getDefault().post(new EBMassage(arrayList, 2)); } mIsChecked.put(goods.getCart_id(), holder.ck.isChecked()); //设置父类的状态 if (mIsChecked.containsValue(false)) { subStatus.set(p, false); } else { subStatus.set(p, true); } //设置爷类改变 if (subStatus.contains(false)) { listState.put(wareHouseList.get(ye), false); } else { listState.put(wareHouseList.get(ye), true); } //设置全选状态 if (listState.containsValue(false)) { shoppingcar_check_box_all.setChecked(false); } else { shoppingcar_check_box_all.setChecked(true); } reflash(); } } ); } holder.count.setText("X" + goods.getBuy_num()); holder.price.setText("¥" + goods.getCprice()); x.image().bind(holder.imgv, goods.getImage()); //设置父类的状态 if (mIsChecked.containsValue(false)) { subStatus.set(p, false); } else { subStatus.set(p, true); } //设置爷类改变 if (subStatus.contains(false)) { listState.put(wareHouseList.get(ye), false); } else { listState.put(wareHouseList.get(ye), true); } //设置全选状态 if (listState.containsValue(false)) { shoppingcar_check_box_all.setChecked(false); } else { shoppingcar_check_box_all.setChecked(true); } reflash(); return convertView; } } //删除物品 private void delete(final Goods goods, final ArrayList datas, final int position) { pb.setVisibility(View.VISIBLE); //删除商品 OkHttpUtils.get().headers(CommonUtils.getHttpHeadParams(getApplication())).url(getUrl(Api.shoppingCarDelete) + "?cart_id=" + goods.getCart_id()).build().execute(new DefaultStringCallBack() { @Override public void onResponse(String response) { pb.setVisibility(View.GONE); try { int code = new JSONObject(response).getInt("code"); if (code == Api.RESPONSE_CODE_SUCESS) { Toast.makeText(getApplication(), "删除成功!", Toast.LENGTH_SHORT).show(); datas.remove(position); if (datas.size() == 0) { shoppingcar_subtitle.setText("编辑"); loadData(); } if (selectGoods.contains(goods)) { ArrayList g = new ArrayList<>(); g.add(goods); EventBus.getDefault().post(new EBMassage(g, 2)); } reflash(); } else { Toast.makeText(getApplication(), "删除出错,错误代码:" + code, Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(Call call, Exception e) { pb.setVisibility(View.GONE); Toast.makeText(getApplication(), "删除商品错误:" + e.getMessage(), Toast.LENGTH_SHORT).show(); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == 110) { selectGoods.clear(); itemIsChecked.clear(); subIsChecked.clear(); listState.clear(); shoppingcar_check_box_all.setChecked(false); shoppingcar_txtv_submit.setText("结算(" + selectGoods.size() + ")"); shoppingcar_txtv_tollPrice.setText("¥0.00"); shoppingcar_txtv_tax.setText("(含关税:¥0,不含运费)"); reflash(); } } //保存所有的Adapter,用于刷新数据 private ArrayList adapterList = new ArrayList<>(); //全选按钮 private void selectAllB() { boolean checked = shoppingcar_check_box_all.isChecked(); for (int i = 0; i < itemIsChecked.size(); i++) { HashMap> arrayLists = itemIsChecked.get(i); Iterator>> iterator1 = arrayLists.entrySet().iterator(); while (iterator1.hasNext()) { HashMap booleen = iterator1.next().getValue(); Iterator> iterator = booleen.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry next = iterator.next(); next.setValue(checked); } } } for (int i = 0; i < subIsChecked.size(); i++) { ArrayList booleen = subIsChecked.get(i); for (int j = 0; j < booleen.size(); j++) { booleen.set(j, shoppingcar_check_box_all.isChecked()); } } for (int i = 0; i < listState.size(); i++) { listState.put(wareHouseList.get(i), shoppingcar_check_box_all.isChecked()); } if (checked) { for (int i = 0; i < cGoodsList.size(); i++) { List> arrayLists = cGoodsList.get(i); for (int j = 0; j < arrayLists.size(); j++) { ArrayList goodses = arrayLists.get(j); EventBus.getDefault().post(new EBMassage(goodses, 1)); } } } else { for (int i = 0; i < cGoodsList.size(); i++) { List> arrayLists = cGoodsList.get(i); for (int j = 0; j < arrayLists.size(); j++) { ArrayList goodses = arrayLists.get(j); EventBus.getDefault().post(new EBMassage(goodses, 2)); } } } } //提交商品 private void submit() { if (selectGoods.size() == 0) { Toast.makeText(getApplication(), "请选择商品", Toast.LENGTH_SHORT).show(); return; } pb.setVisibility(View.VISIBLE); HashMap params = new HashMap<>(); ArrayList list = new ArrayList<>(); params.put("buy_list", list); for (int i = 0; i < selectGoods.size(); i++) { Goods goods = selectGoods.get(i); HashMap goodsMap = new HashMap<>(); goodsMap.put("sku_id", goods.getSku_id() + ""); goodsMap.put("buy_num", goods.getBuy_num() + ""); goodsMap.put("cart_id", goods.getCart_id() + ""); goodsMap.put("comp_id", goods.getComp_id() + ""); goodsMap.put("wh_code", goods.getWh_code()); list.add(goodsMap); } OkHttpUtils .postString() .headers(CommonUtils.getHttpHeadParams(getApplication())) .url(getUrl(Api.preDeal)) .content(JsonUtils.getInstance().obj2Json(params)) .build() .execute(new DefaultStringCallBack() { @Override public void onError(Call call, Exception e) { pb.setVisibility(View.GONE); Toast.makeText(getApplication(), "请求出错,错误信息:" + e.getMessage(), Toast.LENGTH_SHORT).show(); } @Override public void onResponse(String response) { pb.setVisibility(View.GONE); //跳转到订单确认页面 try { int code = new JSONObject(response).getInt("code"); if (code == 200) { Intent intent = new Intent(getActivity(), VerifyOrder.class); intent.putExtra("preStr", response); startActivityForResult(intent, 110); } else { ResponseUtils.checkToken(getApplication(), code); } } catch (JSONException e) { e.printStackTrace(); } } }); } //刷新数据 private void reflash() { for (int i = 0; i < adapterList.size(); i++) { adapterList.get(i).notifyDataSetChanged(); } } //编辑物品的数量 private void editNumb(Goods g, int numb) { pb.setVisibility(View.VISIBLE); isEdited = true; HashMap params = new HashMap<>(); params.put("cart_id", g.getCart_id() + ""); params.put("buy_num", numb + ""); params.put("sku_id", g.getSku_id() + ""); params.put("comp_id", g.getComp_id() + ""); params.put("wh_code", g.getWh_code() + ""); OkHttpUtils.get().headers(CommonUtils.getHttpHeadParams(getApplication())).url(getUrl(Api.shoppingCarEdit)).params(params).build().execute(new DefaultStringCallBack() { @Override public void onError(Call call, Exception e) { pb.setVisibility(View.GONE); Toast.makeText(getApplication(), "编辑数量出错,错误信息:" + e.getMessage(), Toast.LENGTH_SHORT).show(); } @Override public void onResponse(String response) { pb.setVisibility(View.GONE); Toast.makeText(getApplication(), "编辑数量成功!", Toast.LENGTH_SHORT).show(); } }); } @Override public void onResume() { EventBus.getDefault().register(this); super.onResume(); loadData(); } @Override public void onPause() { super.onPause(); EventBus.getDefault().unregister(this); }}
更多相关文章
- 实现 Android(安卓)通知提示功能
- Android电源管理之一:基础概览
- 【Android(安卓)应用开发】Activity 状态保存 OnSaveInstanceSta
- Android通过使用系统广播监听网络状态的改变
- Android(安卓)打电话,发短信,调用系统浏览器
- Android(安卓)的权限设置大全
- Android(安卓)管理Fragments
- Android(安卓)GPS NETWORK定位
- 自定义searchview的编辑框,搜索按钮,删除按钮,光标等