查询购物车的model层
代码语言:javascript复制public class SelectCarModel {
private String url="http://120.27.23.105/product/getCarts";
private HashMap<String, String map = 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<SelectCarBean.DataBean data = selectCarBean.getData();
iSelectCarPresenter.onSuccess(data);
}else{
iSelectCarPresenter.onFailed("请求失败");
}
}
});
}
}
查询购物车的接口
m层的
代码语言:javascript复制public interface ISelectCarPresenter {
void onFailed(String msg);
void onSuccess(List<SelectCarBean.DataBean data);
}
p层的
代码语言:javascript复制public interface ISelectCarView {
void onFailed(String msg);
void onSuccess(List<SelectCarBean.DataBean data);
}
查询购物车的presenter层
代码语言:javascript复制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(List<SelectCarBean.DataBean data) {
iSelectCarView.onSuccess(data);
}
}
查询购物车的view层
代码语言:javascript复制public class ShoppingCartActivity extends AppCompatActivity implements ISelectCarView,View.OnClickListener{
private boolean isLogin;
private SelectCarPresenter selectCarPresenter;
private ExpandableListView car_elv;
private List<Group glist = new ArrayList< ();
private List<List<Child 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<SelectCarBean.DataBean data) {
glist.clear();
clist.clear();
for(int i = 0;i<data.size();i ){
glist.add(new Group(false,data.get(i).getSellerName()));
List<SelectCarBean.DataBean.ListBean list = data.get(i).getList();
List<Child cl = new ArrayList< ();
for (int j = 0; j< list.size(); j ){
cl.add(new Child(false, list.get(j).getTitle(),list.get(j).getPrice(), list.get(j).getImages(), list.get(j).getNum(),list.get(j).getSubhead(),list.get(j).getPid()));
}
clist.add(cl);
}
for(int s = 0; s < carExpandAdapter.getGroupCount(); s ){
car_elv.expandGroup(s);
}
carExpandAdapter.notifyDataSetChanged();
}
@Override
public void onResume() {
super.onResume();
init();
}
/**
* 反选
* @param isAll
*/
public void getisAll(Boolean isAll) {
chooseAll.setChecked(isAll);
}
/**
* 全选按钮
* @param view
*/
@Override
public void onClick(View view) {
isAll = chooseAll.isChecked();
if (!(isAll==true)) {
for (int i = 0; i < glist.size(); i ) {
glist.get(i).setFlag(false);
Log.e("Song","123");
}
for (int i=0 ;i<clist.size();i ){
for (int j=0;j<clist.get(i).size();j ){
clist.get(i).get(j).setFlag(false);
}
}
if (isAll==true){
isAll=false;
}
js();
carExpandAdapter.notifyDataSetChanged();
} else {
for (int i = 0; i < glist.size(); i ) {
glist.get(i).setFlag(true);
}
for (int i=0 ;i<clist.size();i ){
for (int j=0;j<clist.get(i).size();j ){
clist.get(i).get(j).setFlag(true);
}
}
if (isAll==false){
isAll=true;
}
js();
carExpandAdapter.notifyDataSetChanged();
}
}
//结算
public void js(){
p = 0;
int s = 0;//选中商品数量
for(int i=0;i<clist.size();i ){
for(int j=0;j<clist.get(i).size();j ){
if(clist.get(i).get(j).isFlag()){
double price = clist.get(i).get(j).getPrice();
int num = clist.get(i).get(j).getNum();
p = price * num;
s ;
}
}
}
totalPrice.setText("总价:" p "¥");
btnAmount.setText("结算:(" s ")");
}
}
view层布局
代码语言:javascript复制<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
<RelativeLayout
android:id="@ id/car_title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#fff"
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="购物车"
android:textColor="#000"
android:textSize="35dp"
/
<TextView
android:id="@ id/edit_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="编辑"
android:textSize="20dp"
android:layout_marginRight="30dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="30dp" /
</RelativeLayout
<ExpandableListView
android:id="@ id/car_elv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:layout_below="@ id/car_title" </ExpandableListView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_gravity="bottom"
<CheckBox
android:id="@ id/chooseAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:padding="10dp"
android:text="全选"/
<TextView
android:id="@ id/totalPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="合计:0.00 ¥"/
<TextView
android:id="@ id/btnAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结算 : (0)"
android:gravity="center"
android:background="@android:color/holo_orange_light"
android:textColor="@android:color/black"
android:textSize="25sp"
android:padding="10dp"/
</LinearLayout
</LinearLayout
二级购物车列表的适配器
代码语言:javascript复制public class CarExpandAdapter extends BaseExpandableListAdapter{
private Context context;
private List<Group groupList;
private List<List<Child childList;
private ShoppingCartActivity s;
public static boolean flog;
public CarExpandAdapter(Context context, List<Group groupList, List<List<Child 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<groupList.size();i ){
//如果有一个未选中 全选变成FALSE并跳出
if (!groupList.get(i).isFlag()){
s.getisAll(false);
break;
}else{
s.getisAll(true);
}
}
//Child集合布尔值为选中的状态
for (int j=0;j<childList.get(i).size();j ){
childList.get(i).get(j).setFlag(holder.group_box.isChecked());
}
//刷新适配器
notifyDataSetChanged();
s.js();
}
});
return view;
}
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
final ChildViewHodel hodel;
if (view == null){
hodel = new ChildViewHodel();
view = View.inflate(context,R.layout.child_layout,null);
hodel.child_box=view.findViewById(R.id.child_box);
hodel.child_iv = view.findViewById(R.id.child_iv);
hodel.child_tv_name = view.findViewById(R.id.child_tv_name);
hodel.child_tv_attr = view.findViewById(R.id.child_tv_attr);
hodel.child_tv_price = view.findViewById(R.id.child_tv_price);
hodel.child_tv_minus = view.findViewById(R.id.child_tv_minus);
hodel.child_tv_show_num = view.findViewById(R.id.child_tv_show_num);
hodel.child_tv_add = view.findViewById(R.id.child_tv_add);
hodel.child_tv_num = view.findViewById(R.id.child_tv_num);
hodel.delete=view.findViewById(R.id.delete);
view.setTag(hodel);
}else{
hodel = (ChildViewHodel) view.getTag();
}
hodel.child_tv_name.setText(childList.get(i).get(i1).getTitle());
hodel.child_tv_show_num.setText(childList.get(i).get(i1).getNum() "");
hodel.child_tv_attr.setText(childList.get(i).get(i1).getColor());
hodel.child_tv_price.setText(childList.get(i).get(i1).getPrice() "");
Glide.with(context).load(childList.get(i).get(i1).getImage().split("\|")[0]).into(hodel.child_iv);
hodel.child_box.setChecked(childList.get(i).get(i1).isFlag());
//删除的点击事件,根据接口删除的
hodel.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DeletePresenter presenter = new DeletePresenter(new DeleteView() {
@Override
public void onDeleteFailure(String error) {
Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
}
@Override
public void onDeleteSuccesss(String code) {
Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
}
});
int pid = childList.get(i).get(i1).getPid();
presenter.verfiy(3381 "",pid "");
childList.get(i).remove(i1);
// if (childList.get(i).size()==0){
// groupList.remove(i);
// }
notifyDataSetChanged();
}
});
hodel.child_box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Child布尔值变成选中状态
childList.get(i).get(i1).setFlag(hodel.child_box.isChecked());
//遍历Child集合
for (int i=0;i<childList.size();i ){
for (int j=0;j<childList.get(i).size();j ){
//如果有一个未选中 全选与商家按钮变成false并跳出
if(!childList.get(i).get(j).isFlag()){
groupList.get(i).setFlag(false);
s.getisAll(false);
break;
}else{//全部选中 全选与商家按钮变成true
groupList.get(i).setFlag(true);
s.getisAll(false);
}
}
}
//二次遍历
for(int i=0;i<groupList.size();i ){
boolean flag = groupList.get(i).isFlag();
if(flag){
s.getisAll(true);
}else{
s.getisAll(false);
break;
}
}
notifyDataSetChanged();
//计算加减
s.js();
}
});
////
hodel.child_tv_minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String num = hodel.child_tv_show_num.getText().toString();
int num1 = Integer.parseInt(num);
num1--;
childList.get(i).get(i1).setNum(num1);
hodel.child_tv_show_num.setText(childList.get(i).get(i1).getNum() "");
notifyDataSetChanged();
s.js();
}
});
hodel.child_tv_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String num = hodel.child_tv_show_num.getText().toString();
int num1 = Integer.parseInt(num);
num1 ;
childList.get(i).get(i1).setNum(num1);
hodel.child_tv_show_num.setText(childList.get(i).get(i1).getNum() "");
notifyDataSetChanged();
s.js();
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
}
适配器的一级布局
代码语言:javascript复制<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
<CheckBox
android:id="@ id/group_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /
<TextView
android:id="@ id/group_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商家"
/
</LinearLayout
适配器二级布局
代码语言:javascript复制<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
<CheckBox
android:id="@ id/child_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleX="0.6"
android:scaleY="0.6" /
<ImageView
android:id="@ id/child_iv"
android:layout_width="70dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@mipmap/ic_launcher"
android:layout_toRightOf="@id/child_box" /
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="@id/child_iv"
android:orientation="vertical"
<TextView
android:id="@ id/child_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="酒红色纯红色纯羊毛西服套装"
android:textColor="@android:color/black"
android:textSize="12sp"
android:textStyle="bold" /
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
<TextView
android:id="@ id/child_tv_attr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="属性:粉蓝色"
android:textSize="12sp"
android:textColor="@color/colorPrimary" /
</LinearLayout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal"
<TextView
android:id="@ id/child_tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥390"
android:textColor="@android:color/holo_red_dark"
android:textSize="12sp"
android:textStyle="bold" /
<TextView
android:id="@ id/child_tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="x1"
android:textColor="@android:color/darker_gray" /
<LinearLayout
android:id="@ id/rl_edit"
android:layout_width="120dp"
android:background="@android:color/holo_orange_light"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
<TextView
android:id="@ id/child_tv_minus"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="@android:color/black"
android:background="@android:color/white"
android:layout_margin="1dp"
android:layout_height="match_parent"
android:text=" - " /
<TextView
android:id="@ id/child_tv_show_num"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:background="@android:color/white"
android:layout_margin="1dp"
android:layout_height="match_parent"
android:text="1"
/
<TextView
android:id="@ id/child_tv_add"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_margin="1dp"
android:background="@android:color/white"
android:layout_height="match_parent"
android:text=" " /
</LinearLayout
</LinearLayout
</LinearLayout
<Button
android:id="@ id/delete"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="✘"
android:background="@android:color/holo_blue_light"
android:textSize="20sp"
android:textColor="@android:color/holo_green_dark"
android:layout_margin="5dp"
android:visibility="visible" /
</RelativeLayout
bean类Child
代码语言:javascript复制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类
代码语言:javascript复制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请求查询购物车的实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考。