查询购物车的model层
public class SelectCarModel { private String url="http://120.27.23.105/product/getCarts"; private HashMapmap = new HashMap<>(); public void verifySelectCarInfo(int uid, final ISelectCarPresenter iSelectCarPresenter){ map.put("uid",uid+""); OkHttpUtils.getInstance().doPost(url,map, new CallBack() { @Override public void onFailed(String msg) { iSelectCarPresenter.onFailed("请求失败"); } @Override public void onSuccess(String request) { SelectCarBean selectCarBean = GsonUtils.getInstance().fromJson(request, SelectCarBean.class); String code = selectCarBean.getCode(); if ("0".equals(code)){ List data = selectCarBean.getData(); iSelectCarPresenter.onSuccess(data); }else{ iSelectCarPresenter.onFailed("请求失败"); } } }); } }
查询购物车的接口
m层的
public interface ISelectCarPresenter { void onFailed(String msg); void onSuccess(Listdata); }
p层的
public interface ISelectCarView { void onFailed(String msg); void onSuccess(Listdata); }
查询购物车的presenter层
public class SelectCarPresenter implements ISelectCarPresenter{ private ISelectCarView iSelectCarView; private SelectCarModel selectCarModel; public SelectCarPresenter (ISelectCarView iSelectCarView){ this.iSelectCarView = iSelectCarView; selectCarModel = new SelectCarModel(); } //执行集合信息 public void excuteSelectCarData(int uid){ //传到model selectCarModel.verifySelectCarInfo(uid,this); } @Override public void onFailed(String msg) { iSelectCarView.onFailed(msg); } @Override public void onSuccess(Listdata) { iSelectCarView.onSuccess(data); } }
查询购物车的view层
public class ShoppingCartActivity extends AppCompatActivity implements ISelectCarView,View.OnClickListener{ private boolean isLogin; private SelectCarPresenter selectCarPresenter; private ExpandableListView car_elv; private Listglist = new ArrayList<>(); private List > clist = new ArrayList<>(); private CarExpandAdapter carExpandAdapter; private TextView edit_tv; private CheckBox chooseAll; private TextView totalPrice; private TextView btnAmount; private boolean isAll; private double p; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shoppingcar); //初始化数据 init(); chooseAll.setOnClickListener(this); btnAmount.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(ShoppingCartActivity.this, IndentActivity.class); intent.putExtra("price",p+""); startActivity(intent); } }); } /** * 查询购物车 */ private void selectShoppingCar() { selectCarPresenter = new SelectCarPresenter(this); selectCarPresenter.excuteSelectCarData(3381); carExpandAdapter = new CarExpandAdapter(this,glist,clist,this); car_elv.setAdapter(carExpandAdapter); } /** * 初始化属性 */ private void init() { car_elv = (ExpandableListView) findViewById(R.id.car_elv); edit_tv = (TextView) findViewById(R.id.edit_tv); chooseAll = (CheckBox) findViewById(R.id.chooseAll); totalPrice = (TextView) findViewById(R.id.totalPrice); btnAmount = (TextView) findViewById(R.id.btnAmount); //登录购物车 selectShoppingCar(); } @Override public void onFailed(String msg) { } @Override public void onSuccess(List
data) { glist.clear(); clist.clear(); for(int i = 0;i list = data.get(i).getList(); List cl = new ArrayList<>(); for (int j = 0; j
view层布局
二级购物车列表的适配器
public class CarExpandAdapter extends BaseExpandableListAdapter{ private Context context; private ListgroupList; private List > childList; private ShoppingCartActivity s; public static boolean flog; public CarExpandAdapter(Context context, List
groupList, List > childList,ShoppingCartActivity s) { this.cOntext= context; this.groupList = groupList; this.childList = childList; this.s = s; } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int i) { return childList.get(i).size(); } @Override public Object getGroup(int i) { return groupList.get(i); } @Override public Object getChild(int i, int i1) { return childList.get(i).get(i1); } @Override public long getGroupId(int i) { return i; } @Override public long getChildId(int i, int i1) { return i1; } @Override public boolean hasStableIds() { return false; } //第一级列表viewhodel class GroupViewHolder{ CheckBox group_box; TextView group_tv; } //第二级列表的viewhodel class ChildViewHodel{ CheckBox child_box; ImageView child_iv; TextView child_tv_name; TextView child_tv_attr; TextView child_tv_price; TextView child_tv_num; TextView child_tv_minus; TextView child_tv_show_num; TextView child_tv_add; Button delete; } @Override public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) { final GroupViewHolder holder; if (view==null){ holder = new GroupViewHolder(); view = View.inflate(context, R.layout.group_layout,null); holder.group_box = view.findViewById(R.id.group_box); holder.group_tv = view.findViewById(R.id.group_tv); view.setTag(holder); }else{ holder = (GroupViewHolder) view.getTag(); } holder.group_tv.setText(groupList.get(i).getName()); holder.group_box.setChecked(groupList.get(i).isFlag()); holder.group_box.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //group布尔值变成选中状态 groupList.get(i).setFlag(holder.group_box.isChecked()); //遍历Group的集合 for (int i=0;i
适配器的一级布局
适配器二级布局
bean类Child
public class Child { /* false, list.get(j).getTitle(), (float) list.get(j).getPrice(), list.get(j).getImages(), list.get(j).getNum() */ private boolean flag; private String title; private double price; private String image; private int num; private String color; private int pid; public Child(boolean flag, String title, double price, String image, int num,String color,int pid) { this.flag = flag; this.title = title; this.price = price; this.image = image; this.num = num; this.color = color; this.pid = pid; } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } }
group类
public class Group { private boolean flag; private String name; public Group(boolean flag, String name) { this.flag = flag; this.name = name; } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
以上这篇Android Okhttp请求查询购物车的实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。