ShopCartActivity
package com.example.a1512qjd.ui.activity; import android.app.ProgressDialog; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.CheckBox; import android.widget.ExpandableListView; import android.widget.TextView; import com.example.a1512qjd.R; import com.example.a1512qjd.bean.GetCartsBean; import com.example.a1512qjd.bean.SellerBean; import com.example.a1512qjd.presenter.GetCartsPresenterImp; import com.example.a1512qjd.ui.adapter.ElvShopcartAdapter; import com.example.a1512qjd.ui.inter.GetCartsView; import com.example.a1512qjd.utils.DialogUtil; import java.util.List; public class ShopCartActivity extends AppCompatActivity implements GetCartsView { private ExpandableListView mElv; private GetCartsPresenterImp getCartsPresenterImp; private ProgressDialog progressDialog; /** * 全选 */ private CheckBox mCbAll; /** * 合计: */ private TextView mTvMoney; /** * 去结算: */ private TextView mTvTotal; private ElvShopcartAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shop_cart); //初始化dialog progressDialog = DialogUtil.getProgressDialog(this); initView(); //绑定 getCartsPresenterImp = new GetCartsPresenterImp(this); SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE); String uid = sharedPreferences.getString("uid", "-1"); String token = sharedPreferences.getString("token", ""); getCartsPresenterImp.getCarts(uid, token); } @Override protected void onDestroy() { super.onDestroy(); getCartsPresenterImp.detach(); } private void initView() { mElv = (ExpandableListView) findViewById(R.id.elv); mCbAll = (CheckBox) findViewById(R.id.cbAll); mTvMoney = (TextView) findViewById(R.id.tvMoney); mTvTotal = (TextView) findViewById(R.id.tvTotal); mCbAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (adapter != null) { progressDialog.show(); adapter.changeAllState(mCbAll.isChecked()); } } }); } @Override public void showData(List<SellerBean> groupList, List<List<GetCartsBean.DataBean.ListBean>> childList) { //判断所有商家是否全部选中 mCbAll.setChecked(isSellerAddSelected(groupList)); //创建适配器 adapter = new ElvShopcartAdapter(this, groupList, childList, getCartsPresenterImp, progressDialog); mElv.setAdapter(adapter); //获取数量和总价 String[] strings = adapter.computeMoneyAndNum(); mTvMoney.setText("总计:" + strings[0] + "元"); mTvTotal.setText("去结算("+strings[1]+"个)"); // //默认展开列表 for (int i = 0; i < groupList.size(); i++) { mElv.expandGroup(i); } //关闭进度条 progressDialog.dismiss(); } /** * 判断所有商家是否全部选中 * * @param groupList * @return */ private boolean isSellerAddSelected(List<SellerBean> groupList) { for (int i = 0; i < groupList.size(); i++) { SellerBean sellerBean = groupList.get(i); if (!sellerBean.isSelected()) { return false; } } return true; }
}
activity_shop_cart.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".ui.activity.ShopCartActivity"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:text="购物车"/> <ExpandableListView android:id="@+id/elv" android:groupIndicator="@null" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"></ExpandableListView> <RelativeLayout android:layout_width="match_parent" android:layout_height="40dp"> <CheckBox android:id="@+id/cbAll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="全选"/> <TextView android:id="@+id/tvMoney" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/cbAll" android:text="合计:"/> <TextView android:id="@+id/tvTotal" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:textColor="#ffffff" android:background="#ff0000" android:gravity="center" android:layout_marginLeft="10dp" android:text="去结算:"/> </RelativeLayout> </LinearLayout>
GetCartsModel
package com.example.a1512qjd.model.inter; import com.example.a1512qjd.net.OnNetListener; public interface GetCartsModel { void getCarts(String uid, String token, OnNetListener onNetListener);
}
GetCartsModelImp
package com.example.a1512qjd.model; import com.example.a1512qjd.bean.GetCartsBean; import com.example.a1512qjd.model.inter.GetCartsModel; import com.example.a1512qjd.net.Api; import com.example.a1512qjd.net.OkhttpUtils; import com.example.a1512qjd.net.OnNetListener; import com.google.gson.Gson; import java.util.HashMap; import java.util.Map; public class GetCartsModelImp implements GetCartsModel { @Override public void getCarts(String uid, String token, final OnNetListener onNetListener) { Map<String, String> params = new HashMap<>(); params.put("uid", uid); params.put("token", token); OkhttpUtils.getInstance().doPost(Api.CARTS_URL, params, new OnNetListener() { @Override public void onSuccess(String result) { GetCartsBean getCartsBean = new Gson().fromJson(result, GetCartsBean.class); if ("0".equals(getCartsBean.getCode())) { onNetListener.onSuccess(result); } } @Override public void onFailed(Exception e) { onNetListener.onFailed(e); } }); }
}
GetCartsView
package com.example.a1512qjd.ui.inter; import com.example.a1512qjd.bean.GetCartsBean; import com.example.a1512qjd.bean.SellerBean; import java.util.List; public interface GetCartsView { void showData(List<SellerBean> groupList, List<List<GetCartsBean.DataBean.ListBean>> childList);
}
GetCartsPresenter
package com.example.a1512qjd.presenter.inter; public interface GetCartsPresenter { void getCarts(String uid, String token);
}
GetCartsPresenterImp
package com.example.a1512qjd.presenter; import com.example.a1512qjd.bean.GetCartsBean; import com.example.a1512qjd.bean.SellerBean; import com.example.a1512qjd.model.GetCartsModelImp; import com.example.a1512qjd.net.OnNetListener; import com.example.a1512qjd.presenter.inter.GetCartsPresenter; import com.example.a1512qjd.ui.inter.GetCartsView; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; public class GetCartsPresenterImp implements GetCartsPresenter { private final GetCartsModelImp getCartsModelImp; private GetCartsView getCartsView; public GetCartsPresenterImp(GetCartsView getCartsView) { this.getCartsView = getCartsView; getCartsModelImp = new GetCartsModelImp(); } @Override public void getCarts(String uid, String token) { getCartsModelImp.getCarts(uid, token, new OnNetListener() { @Override public void onSuccess(String result) { if (getCartsView != null) { List<SellerBean> groupList = new ArrayList<>();//用于封装一级列表 List<List<GetCartsBean.DataBean.ListBean>> childList = new ArrayList<>();//用于封装二级列表 GetCartsBean getCartsBean = new Gson().fromJson(result, GetCartsBean.class); List<GetCartsBean.DataBean> data = getCartsBean.getData(); for (int i = 0; i < data.size(); i++) { GetCartsBean.DataBean dataBean = data.get(i); SellerBean sellerBean = new SellerBean(); sellerBean.setSellerName(dataBean.getSellerName()); sellerBean.setSellerid(dataBean.getSellerid()); sellerBean.setSelected(isSellerProductAllSelect(dataBean)); //true或者false要根据该商家下面的商品是否全选 groupList.add(sellerBean); List<GetCartsBean.DataBean.ListBean> list = dataBean.getList(); childList.add(list); } getCartsView.showData(groupList, childList); } } @Override public void onFailed(Exception e) { } }); } /** * 判断该商家下面的商品是否全选 * * @return */ private boolean isSellerProductAllSelect(GetCartsBean.DataBean dataBean) { //获取该商家下面的所有商品 List<GetCartsBean.DataBean.ListBean> list = dataBean.getList(); for (int i = 0; i < list.size(); i++) { GetCartsBean.DataBean.ListBean listBean = list.get(i); if (0 == listBean.getSelected()) { //如果是0的话,表示有一个商品未选中 return false; } } return true; } public void detach() { if (getCartsView != null) { getCartsView = null; } } }
ElvShopCartAdapter
package com.example.a1512qjd.ui.adapter; import android.app.ProgressDialog; import android.content.Context; import android.content.SharedPreferences; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.bumptech.glide.Glide; import com.example.a1512qjd.R; import com.example.a1512qjd.bean.GetCartsBean; import com.example.a1512qjd.bean.SellerBean; import com.example.a1512qjd.presenter.DeleteCartPresenterImp; import com.example.a1512qjd.presenter.GetCartsPresenterImp; import com.example.a1512qjd.presenter.UpdateCartsPresenterImp; import com.example.a1512qjd.ui.inter.DeleteCartView; import com.example.a1512qjd.ui.inter.UpdateCartsView; import com.example.a1512qjd.ui.widget.AddSubView; import java.util.List; public class ElvShopcartAdapter extends BaseExpandableListAdapter implements UpdateCartsView, DeleteCartView { private Context context; private List<SellerBean> groupList; private List<List<GetCartsBean.DataBean.ListBean>> childList; private LayoutInflater inflater; private final UpdateCartsPresenterImp updateCartsPresenterImp; private GetCartsPresenterImp getCartsPresenterImp; private final String uid; private final String token; private ProgressDialog progressDialog; private int productIndex; private int groupPosition; private boolean checked; private static final int GETCARTS = 0;//查询购物车 private static final int UPDATE_PRODUCT = 1; //更新商品 private static final int UPDATE_SELLER = 2; //更新卖家 private static int state = GETCARTS; private boolean allSelected; private final DeleteCartPresenterImp deleteCartPresenterImp; public ElvShopcartAdapter(Context context, List<SellerBean> groupList, List<List<GetCartsBean.DataBean.ListBean>> childList, GetCartsPresenterImp getCartsPresenterImp, ProgressDialog progressDialog) { this.context = context; this.groupList = groupList; this.childList = childList; inflater = LayoutInflater.from(context); this.getCartsPresenterImp = getCartsPresenterImp; //绑定 updateCartsPresenterImp = new UpdateCartsPresenterImp(this); deleteCartPresenterImp = new DeleteCartPresenterImp(this); SharedPreferences sharedPreferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); uid = sharedPreferences.getString("uid", "-1"); token = sharedPreferences.getString("token", ""); //初始化进度对话框 this.progressDialog = progressDialog; } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { return childList.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return childList.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { final GroupViewHolder groupViewHolder; if (convertView == null) { groupViewHolder = new GroupViewHolder(); convertView = inflater.inflate(R.layout.shopcart_seller_item, null); groupViewHolder.cbSeller = convertView.findViewById(R.id.cbSeller); groupViewHolder.tvSeller = convertView.findViewById(R.id.tvSeller); convertView.setTag(groupViewHolder); } else { groupViewHolder = (GroupViewHolder) convertView.getTag(); } //设置值 SellerBean sellerBean = groupList.get(groupPosition); groupViewHolder.tvSeller.setText(sellerBean.getSellerName()); groupViewHolder.cbSeller.setChecked(sellerBean.isSelected()); //给商家checkbox设置点击事件 groupViewHolder.cbSeller.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //设置当前的更新状态 state = UPDATE_PRODUCT; //显示进度条 progressDialog.show(); //默认从第一个商品开始更新购物车状态 productIndex = 0; //全局记录一下当前更新的商家 ElvShopcartAdapter.this.groupPosition = groupPosition; //该商家是否选中 checked = groupViewHolder.cbSeller.isChecked(); //更新商家下的商品状态 updateProductInSeller(); } }); return convertView; } @Override public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final ChildViewHolder childViewHolder; if (convertView == null) { childViewHolder = new ChildViewHolder(); convertView = inflater.inflate(R.layout.shopcart_seller_product_item, null); childViewHolder.cbProduct = convertView.findViewById(R.id.cbProduct); childViewHolder.iv = convertView.findViewById(R.id.iv); childViewHolder.tvTitle = convertView.findViewById(R.id.tvTitle); childViewHolder.tvPrice = convertView.findViewById(R.id.tvPrice); childViewHolder.tvDel = convertView.findViewById(R.id.tvDel); childViewHolder.addSubView = convertView.findViewById(R.id.addSubCard); convertView.setTag(childViewHolder); } else { childViewHolder = (ChildViewHolder) convertView.getTag(); } final GetCartsBean.DataBean.ListBean listBean = childList.get(groupPosition).get(childPosition); //根据服务器返回的select值,给checkBox设置是否选中 childViewHolder.cbProduct.setChecked(listBean.getSelected() == 1 ? true : false); childViewHolder.tvTitle.setText(listBean.getTitle()); childViewHolder.tvPrice.setText(listBean.getPrice() + ""); Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(childViewHolder.iv); childViewHolder.addSubView.setNum(listBean.getNum() + ""); //给二级列表的checkbox设置点击事件 childViewHolder.cbProduct.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { state = GETCARTS; //显示进度条 progressDialog.show(); ElvShopcartAdapter.this.groupPosition = groupPosition; //调用更新购物车接口,改变购物车的状态 //获取卖家id String sellerid = groupList.get(groupPosition).getSellerid(); //获取pid String pid = listBean.getPid() + ""; //是否选中 boolean childChecked = childViewHolder.cbProduct.isChecked(); updateCartsPresenterImp.updateCarts(uid, sellerid, pid, "1", childChecked ? "1" : "0", token); } }); //给加号设置点击事件 childViewHolder.addSubView.setAddOnclickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressDialog.show(); state = GETCARTS; //获取sellerId String sellerid = groupList.get(groupPosition).getSellerid(); //获取pid int pid = listBean.getPid(); //获取数量 int num = listBean.getNum(); num += 1; //是否选中 String isChecked = childViewHolder.cbProduct.isChecked() ? "1" : "0"; //调用更新购物车的接口即可 updateCartsPresenterImp.updateCarts(uid, sellerid, pid + "", num + "", isChecked, token); } }); //给减号设置点击事件 childViewHolder.addSubView.setSubOnclickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressDialog.show(); state = GETCARTS; //获取当前商品的数量 int num = listBean.getNum(); if (num <= 1) { progressDialog.dismiss(); Toast.makeText(context, "数量不能小于1", Toast.LENGTH_SHORT).show(); return; } num -= 1; //获取sellerId String sellerid = groupList.get(groupPosition).getSellerid(); //获取pid int pid = listBean.getPid(); //是否选中 String isChecked = childViewHolder.cbProduct.isChecked() ? "1" : "0"; //更新购物车 updateCartsPresenterImp.updateCarts(uid, sellerid, pid + "", num + "", isChecked, token); } }); //给删除设置点击事件 childViewHolder.tvDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressDialog.show(); state = GETCARTS; //获取pid int pid = listBean.getPid(); //删除购物车里的选项 deleteCartPresenterImp.deleteCart(uid, pid + "", token); } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } //删除成功回调接口 @Override public void delSuccess() { getCartsPresenterImp.getCarts(uid, token); } class GroupViewHolder { CheckBox cbSeller; TextView tvSeller; } class ChildViewHolder { CheckBox cbProduct; ImageView iv; TextView tvTitle; TextView tvPrice; TextView tvDel; AddSubView addSubView; } //更新购物车成功回调的方法 @Override public void updataSuccess() { switch (state) { case GETCARTS: //更新成功以后调用查询购物车接口 productIndex = 0; groupPosition = 0; getCartsPresenterImp.getCarts(uid, token); break; case UPDATE_PRODUCT: //更新成功一个商品以后,再接着更新该商家下面的其它商品,直到没有商品为止 productIndex++; //下标是否越界 if (productIndex < childList.get(groupPosition).size()) { //可以继续跟新商品 updateProductInSeller(); } else { //商品已经全部更新完成,请查询购物车 state = GETCARTS; updataSuccess(); } break; case UPDATE_SELLER: //遍历所有商家下的商品,并更新状态 productIndex++; //下标是否越界 if (productIndex < childList.get(groupPosition).size()) { //可以继续跟新商品 updateProductInSeller(allSelected); } else { //商品已经全部更新完成,请查询购物车 productIndex = 0; groupPosition++; if (groupPosition < groupList.size()) { //可以继续跟新商品 updateProductInSeller(allSelected); } else { //商品已经全部更新完成,请查询购物车 state = GETCARTS; updataSuccess(); } } break; } } /** * 更新购物车列表成功回调的方法 */ // @Override // public void showData() { // progressDialog.dismiss(); // switch (state) { // case UPDATE_PRODUCT: // productIndex++; // //获取对应商家下的商品数量 // int size = childList.get(groupPosition).size(); // if (productIndex < size) { // //继续更新改商家的其它商品状态 // updateProductInSeller(); // } else { // state = GETCARTS; // showData(); // } // break; // case UPDATE_SELLER: // productIndex++; // if (productIndex < childList.get(groupPosition).size()) { // //继续更新改商家的其它商品状态 // updateProductInSeller(allSelected); // } else { // productIndex = 0; // groupPosition++; // if (groupPosition < childList.size()) { // updateProductInSeller(allSelected); // } else { // state = GETCARTS; // showData(); // } // } // break; // case GETCARTS: // //把商品下表还原成默认值0 // productIndex = 0; // //把商家下表还原成默认值0 // groupPosition = 0; // getCartsPresenterImp.getCarts(uid, token); // break; // } // // // } private void updateProductInSeller() { //获取SellerId SellerBean sellerBean = groupList.get(groupPosition); String sellerid = sellerBean.getSellerid(); //获取pid GetCartsBean.DataBean.ListBean listBean = childList.get(groupPosition).get(productIndex); int num = listBean.getNum(); int pid = listBean.getPid(); updateCartsPresenterImp.updateCarts(uid, sellerid, pid + "", num + "", checked ? "1" : "0", token); } private void updateProductInSeller(boolean bool) { //获取SellerId SellerBean sellerBean = groupList.get(groupPosition); String sellerid = sellerBean.getSellerid(); //获取pid GetCartsBean.DataBean.ListBean listBean = childList.get(groupPosition).get(productIndex); int pid = listBean.getPid(); int num = listBean.getNum(); updateCartsPresenterImp.updateCarts(uid, sellerid, pid + "", num + "", bool ? "1" : "0", token); } /** * 计算数量和价钱 * * @return */ public String[] computeMoneyAndNum() { double sum = 0; int num = 0; for (int i = 0; i < groupList.size(); i++) { for (int j = 0; j < childList.get(i).size(); j++) { //判断商品是否选中 GetCartsBean.DataBean.ListBean listBean = childList.get(i).get(j); if (listBean.getSelected() == 1) { //该商品为选中状态 sum += listBean.getPrice() * listBean.getNum(); num += listBean.getNum(); } } } return new String[]{sum + "", num + ""}; } public void changeAllState(boolean bool) { this.allSelected = bool; state = UPDATE_SELLER; //遍历商家下的商品,修改状态 updateProductInSeller(bool); } /* public void changeAllState(boolean bool) { this.allSelected = bool; state = UPDATE_SELLER; updateProductInSeller(bool); }*/
}
shopcart_seller_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="30dp" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/cbSeller" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp"/> <TextView android:id="@+id/tvSeller" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp"/>
</LinearLayout>
shopcart_seller_product_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/cbProduct" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp"/> <TextView android:id="@+id/tvDel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:background="#ff0000" android:gravity="center_vertical" android:text="删除" android:textColor="#ffffff"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@id/tvDel" android:layout_toRightOf="@id/cbProduct" android:orientation="horizontal"> <ImageView android:id="@+id/iv" android:layout_width="70dp" android:layout_height="70dp" android:layout_gravity="center_vertical"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:orientation="vertical"> <TextView android:id="@+id/tvTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tvPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="12312" android:textColor="#ff0000"/> <com.example.a1512qjd.ui.widget.AddSubView android:id="@+id/addSubCard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true"></com.example.a1512qjd.ui.widget.AddSubView> </RelativeLayout> </LinearLayout> </LinearLayout>
</RelativeLayout>
DeleteCartModel
package com.example.a1512qjd.model.inter; import com.example.a1512qjd.net.OnNetListener; public interface DeleteCartModel { void deleteCart(String uid, String pid, String token, OnNetListener onNetListener);
}
DeleteCartModelImp
package com.example.a1512qjd.model; import com.example.a1512qjd.model.inter.DeleteCartModel; import com.example.a1512qjd.net.Api; import com.example.a1512qjd.net.OkhttpUtils; import com.example.a1512qjd.net.OnNetListener; import java.util.HashMap; import java.util.Map; public class DeleteCartModelImp implements DeleteCartModel { @Override public void deleteCart(String uid, String pid, String token, final OnNetListener onNetListener) { Map<String,String> params = new HashMap<>(); params.put("uid",uid); params.put("pid",pid); params.put("token",token); OkhttpUtils.getInstance().doPost(Api.DELETECART_URL, params, new OnNetListener() { @Override public void onSuccess(String result) { onNetListener.onSuccess(result); } @Override public void onFailed(Exception e) { onNetListener.onFailed(e); } }); }
}
DeleteCartView
package com.example.a1512qjd.ui.inter; public interface DeleteCartView { void delSuccess();
}
DeleteCartPresenter
package com.example.a1512qjd.presenter.inter; public interface DeleteCartPresenter { void deleteCart(String uid, String pid, String token);
}
DeleteCartPresenterImp
package com.example.a1512qjd.presenter; import com.example.a1512qjd.model.DeleteCartModelImp; import com.example.a1512qjd.net.OnNetListener; import com.example.a1512qjd.presenter.inter.DeleteCartPresenter; import com.example.a1512qjd.ui.inter.DeleteCartView; public class DeleteCartPresenterImp implements DeleteCartPresenter { private final DeleteCartModelImp deleteCartModelImp; private DeleteCartView deleteCartView; public DeleteCartPresenterImp(DeleteCartView deleteCartView) { this.deleteCartView = deleteCartView; deleteCartModelImp = new DeleteCartModelImp(); } @Override public void deleteCart(String uid, String pid, String token) { deleteCartModelImp.deleteCart(uid, pid, token, new OnNetListener() { @Override public void onSuccess(String result) { deleteCartView.delSuccess(); } @Override public void onFailed(Exception e) { } }); } }
LoginActivity
package com.example.a1512qjd.ui.activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.example.a1512qjd.R; import com.example.a1512qjd.bean.UserBean; import com.example.a1512qjd.presenter.LoginPresenterImp; import com.example.a1512qjd.ui.inter.LoginView; public class LoginActivity extends AppCompatActivity implements View.OnClickListener, LoginView { private RelativeLayout mLoginTitleRelative; /** * 请输入手机号 */ private EditText mEditPhone; /** * 请输入密码 */ private EditText mEditPwd; /** * 手机快速注册 */ private TextView mTextRegist; private ImageView mLoginByWechat; private ImageView mLoginByQq; private LoginPresenterImp imp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initView(); //绑定 imp = new LoginPresenterImp(this); } private void initView() { mLoginTitleRelative = (RelativeLayout) findViewById(R.id.login_title_relative); mEditPhone = (EditText) findViewById(R.id.edit_phone); mEditPwd = (EditText) findViewById(R.id.edit_pwd); mTextRegist = (TextView) findViewById(R.id.text_regist); mTextRegist.setOnClickListener(this); mLoginByWechat = (ImageView) findViewById(R.id.login_by_wechat); mLoginByQq = (ImageView) findViewById(R.id.login_by_qq); mLoginByQq.setOnClickListener(this); } //登录 public void login(View view) { //获取用户名和密码 String mobile = mEditPhone.getText().toString(); String password = mEditPwd.getText().toString(); imp.login(mobile, password); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.text_regist: break; case R.id.login_by_qq: break; } } @Override public void loginSuccess(UserBean userBean) { //保存用户信息到SharedPreferences SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE); SharedPreferences.Editor edit = sharedPreferences.edit(); edit.putString("uid", userBean.getData().getUid() + ""); edit.putString("name", userBean.getData().getUsername() + ""); edit.putString("iconUrl", userBean.getData().getIcon() + ""); edit.putString("token", userBean.getData().getToken() + ""); edit.commit(); //关闭当前页面 this.finish(); }
}
activity_login.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/login_title_relative" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/cha"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="京东登录"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/login_title_relative" android:layout_margin="10dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号"/> <EditText android:id="@+id/edit_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入手机号"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码"/> <EditText android:id="@+id/edit_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword" android:singleLine="true"/> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:gravity="center" android:onClick="login" android:text="登录"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal"> <TextView android:id="@+id/text_regist" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="手机快速注册"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="忘记密码"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="20dp" android:orientation="horizontal"> <ImageView android:id="@+id/login_by_wechat" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/umeng_socialize_wechat"/> <ImageView android:id="@+id/login_by_qq" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/umeng_socialize_qq"/> </LinearLayout>
</RelativeLayout>
LoginModel
package com.example.a1512qjd.model.inter; import com.example.a1512qjd.net.OnNetListener; public interface LoginModel { void login(String mobile, String password, OnNetListener onNetListener);
}
LoginModelImp
package com.example.a1512qjd.model; import com.example.a1512qjd.bean.UserBean; import com.example.a1512qjd.model.inter.LoginModel; import com.example.a1512qjd.net.Api; import com.example.a1512qjd.net.OkhttpUtils; import com.example.a1512qjd.net.OnNetListener; import com.google.gson.Gson; import java.util.HashMap; import java.util.Map; public class LoginModelImp implements LoginModel { @Override public void login(String mobile, String password, final OnNetListener onNetListener) { Map<String, String> params = new HashMap<>(); params.put("mobile", mobile); params.put("password", password); OkhttpUtils.getInstance().doPost(Api.LOGIN_URL, params, new OnNetListener() { @Override public void onSuccess(String result) { UserBean userBean = new Gson().fromJson(result, UserBean.class); if ("0".equals(userBean.getCode())) { onNetListener.onSuccess(result); } } @Override public void onFailed(Exception e) { onNetListener.onFailed(e); } }); } }
LoginView
package com.example.a1512qjd.ui.inter; import com.example.a1512qjd.bean.UserBean; public interface LoginView { void loginSuccess(UserBean userBean);
}
LoginPresenter
package com.example.a1512qjd.presenter.inter; public interface LoginPresenter { void login(String mobile, String password);
}
LoginPresenterImp
package com.example.a1512qjd.presenter; import com.example.a1512qjd.bean.UserBean; import com.example.a1512qjd.model.LoginModelImp; import com.example.a1512qjd.net.OnNetListener; import com.example.a1512qjd.presenter.inter.LoginPresenter; import com.example.a1512qjd.ui.inter.LoginView; import com.google.gson.Gson; public class LoginPresenterImp implements LoginPresenter { private LoginView loginView; private final LoginModelImp loginModelImp; public LoginPresenterImp(LoginView loginView) { this.loginView = loginView; loginModelImp = new LoginModelImp(); } @Override public void login(String mobile, String password) { //判断账号密码是否合法 loginModelImp.login(mobile, password, new OnNetListener() { @Override public void onSuccess(String result) { if (loginView != null) { UserBean userBean = new Gson().fromJson(result, UserBean.class); loginView.loginSuccess(userBean); } } @Override public void onFailed(Exception e) { } }); } public void detach() { if (loginView != null) { loginView = null; } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/cbProduct" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp"/> <TextView android:id="@+id/tvDel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:background="#ff0000" android:gravity="center_vertical" android:text="删除" android:textColor="#ffffff"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@id/tvDel" android:layout_toRightOf="@id/cbProduct" android:orientation="horizontal"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/iv" android:layout_width="70dp" android:layout_height="70dp" android:layout_gravity="center_vertical"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:orientation="vertical"> <TextView android:id="@+id/tvTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tvPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="12312" android:textColor="#ff0000"/> <com.example.john.jd_demo.widgt.ASview android:id="@+id/asCard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true"></com.example.john.jd_demo.widgt.ASview> </RelativeLayout> </LinearLayout> </LinearLayout> </RelativeLayout>
SellerBean
package com.example.john.jd_demo.bean; /** * Created by john on 2018/6/22. */ public class MerChantBean { private String MerChantName; private String MerChantId; private boolean MerChantSelected;//商家是否选中 public String getMerChantName() { return MerChantName; } public void setMerChantName(String merChantName) { MerChantName = merChantName; } public String getMerChantId() { return MerChantId; } public void setMerChantId(String merChantId) { MerChantId = merChantId; } public boolean isMerChantSelected() { return MerChantSelected; } public void setMerChantSelected(boolean merChantSelected) { MerChantSelected = merChantSelected; }
}
GetCartBean
package com.example.john.jd_demo.bean; import java.util.List; public class GetCartBean { private String msg; private String code; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { private String sellerName; private String sellerid; private List<ListBean> list; public String getSellerName() { return sellerName; } public void setSellerName(String sellerName) { this.sellerName = sellerName; } public String getSellerid() { return sellerid; } public void setSellerid(String sellerid) { this.sellerid = sellerid; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } public static class ListBean { private double bargainPrice; private String createtime; private String detailUrl; private String images; private int num; private int pid; private double price; private int pscid; private int selected; private int sellerid; private String subhead; private String title; public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSelected() { return selected; } public void setSelected(int selected) { this.selected = selected; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } } }