Android Okhttp請求查詢購物車的實例代碼
更新時間:2018年01月19日 15:35:31 作者:Android10001
下面小編就為大家分享一篇Android Okhttp請求查詢購物車的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
查詢購物車的model層
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層的
public interface ISelectCarPresenter { void onFailed(String msg); void onSuccess(List<SelectCarBean.DataBean> data); }
p層的
public interface ISelectCarView { void onFailed(String msg); void onSuccess(List<SelectCarBean.DataBean> data); }
查詢購物車的presenter層
public class SelectCarPresenter implements ISelectCarPresenter{ private ISelectCarView iSelectCarView; private SelectCarModel selectCarModel; public SelectCarPresenter (ISelectCarView iSelectCarView){ this.iSelectCarView = iSelectCarView; selectCarModel = new SelectCarModel(); } //執(zhí)行集合信息 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層
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); //初始化數(shù)據(jù) 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;//選中商品數(shù)量 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層布局
<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>
二級購物車列表的適配器
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布爾值變成選中狀態(tài) 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集合布爾值為選中的狀態(tài) 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()); //刪除的點擊事件,根據(jù)接口刪除的 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布爾值變成選中狀態(tài) 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; } }
適配器的一級布局
<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>
適配器二級布局
<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
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請求查詢購物車的實例代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android中使用OkHttp包處理HTTP的get和post請求的方法
- Android使用OkHttp發(fā)送post請求
- Android小知識之OkHttp的2種請求方式詳解
- Android使用OkHttp請求自簽名的https網(wǎng)站的示例
- Android基于OkHttpUtils網(wǎng)絡請求的二次封裝
- 詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法
- Android M(6.x)使用OkHttp包解析和發(fā)送JSON請求的教程
- 詳解Android的OkHttp包編寫異步HTTP請求調用的方法
- android實現(xiàn)okHttp的get和post請求的簡單封裝與使用
相關文章
Android編程使用Service實現(xiàn)Notification定時發(fā)送功能示例
這篇文章主要介紹了Android編程使用Service實現(xiàn)Notification定時發(fā)送功能,涉及Android服務Service控制通知的發(fā)送功能相關操作技巧,需要的朋友可以參考下2017-08-08Android編程圖片加載類ImageLoader定義與用法實例分析
這篇文章主要介紹了Android編程圖片加載類ImageLoader定義與用法,結合實例形式分析了Android圖片加載類ImageLoader的功能、定義、使用方法及相關操作注意事項,代碼中備有較為詳盡的注釋便于理解,需要的朋友可以參考下2017-12-12Android中使用TextView實現(xiàn)圖文混排的方法
向TextView或EditText中添加圖像比直接添加文本復雜一點點,需要用到<img>標簽。接下來通過本文給大家介紹Android中使用TextView實現(xiàn)圖文混排的方法,希望對大家有所幫助2016-02-02Android訪問php取回json數(shù)據(jù)實例
Android訪問php取回json數(shù)據(jù),實現(xiàn)代碼如下,遇到訪問網(wǎng)絡的權限不足在AndroidManifest.xml中,需要進行如下配置2013-06-06