From 0d662c0ad61cdd1059f26b083242165b1cec837e Mon Sep 17 00:00:00 2001 From: ghtMare <2467889729@qq.com> Date: Sun, 14 May 2023 21:25:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=AD=E7=89=A9=E8=BD=A6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=95=86=E5=93=81=E5=92=8C=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CatShoppingCartController.java | 46 +++--- .../cat/mapper/ShoppingCartMapper.java | 16 ++- .../cat/service/ShoppingCartService.java | 26 ++-- .../resources/mapper/ShoppingCartMapper.xml | 24 ++-- .../catapp/activity/IndexActivity.java | 6 +- .../example/catapp/adapter/ShopAdapter.java | 32 +++-- .../catapp/adapter/ShoppingCartAdapter.java | 4 +- .../catapp/entity/ShoppingCartPack.java | 2 +- .../catapp/fragment/IndexFragment.java | 1 + .../fragment/ProductDetailFragment.java | 85 ++++++++++++ .../catapp/fragment/ProductFragment.java | 46 +++++- .../catapp/fragment/ShoppingCartFragment.java | 119 +++++++++++----- .../catapp/netrequest/OkHttpShoppingcart.java | 57 +++++++- .../src/main/res/layout/cart_buttonlayout.xml | 46 ++++++ .../src/main/res/layout/cart_have_product.xml | 20 ++- .../app/src/main/res/layout/good_detail.xml | 82 +++++++++++ .../app/src/main/res/layout/item.xml | 131 ++++++++++-------- .../layout/shoppingcart_detail_content.xml | 12 +- orange.sql | 34 +++-- 19 files changed, 606 insertions(+), 183 deletions(-) create mode 100644 CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ProductDetailFragment.java create mode 100644 CatApp_android_frontEnd/app/src/main/res/layout/cart_buttonlayout.xml create mode 100644 CatApp_android_frontEnd/app/src/main/res/layout/good_detail.xml diff --git a/CatApp_android_End/src/main/java/com/example/cat/controller/CatShoppingCartController.java b/CatApp_android_End/src/main/java/com/example/cat/controller/CatShoppingCartController.java index 39524ee..6743550 100644 --- a/CatApp_android_End/src/main/java/com/example/cat/controller/CatShoppingCartController.java +++ b/CatApp_android_End/src/main/java/com/example/cat/controller/CatShoppingCartController.java @@ -3,6 +3,7 @@ package com.example.cat.controller; import com.example.cat.entity.CatProduct; import com.example.cat.entity.CatShoppingCart; import com.example.cat.service.ShoppingCartService; +import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.CollectionUtils; @@ -20,7 +21,7 @@ import java.util.Objects; * @Date 2023/5/9 */ @RestController -@RequestMapping("/orange/shoppingcart") +@RequestMapping("/orange/shopping") @CrossOrigin public class CatShoppingCartController { @Autowired @@ -66,8 +67,6 @@ public class CatShoppingCartController { } map.put("flag", true); map.put("data", productList); - System.out.println("1111"+productList.get(1).getNum()); - System.out.println("1111"+productList.get(0).getNum()); return map; } /** @@ -97,44 +96,49 @@ public class CatShoppingCartController { } } - @RequestMapping(value = "/deleteShop", method = RequestMethod.POST) - public Map deleteShoppingCart(@RequestBody String id) { - Integer deleteFlag = shoppingCartService.deleteShoppingCart(Integer.valueOf(id)); + + @RequestMapping(value = "/addS", method = RequestMethod.POST) + public Map addProduct(@Param("userId") String userId,@Param("productId") String productId) { + Integer addFlag = shoppingCartService.addShop(Integer.valueOf(userId),Integer.valueOf(productId)); Map map = new HashMap<>(); - if (deleteFlag <= 0) { + if (addFlag <= 0) { map.put("flag", false); - map.put("msg", "delete error"); + map.put("msg", "add error"); return map; } map.put("flag", true); - map.put("msg", "delete success"); + map.put("msg", "add success"); + System.out.println("addS--------"); return map; } - @RequestMapping(value = "/updateShop", method = RequestMethod.POST) - public Map updateShoppingCart(@RequestBody String num,@RequestBody String id) { - Integer updateFlag = shoppingCartService.updateShoppingCart(Integer.valueOf(num),Integer.valueOf(id)); + @RequestMapping(value = "/deleteS", method = RequestMethod.POST) + public Map deleteProduct(@Param("userId") String userId,@Param("productId") String productId) { + Integer deleteFlag = shoppingCartService.deleteShop(Integer.valueOf(userId),Integer.valueOf(productId)); Map map = new HashMap<>(); - if (updateFlag <= 0) { + + if (deleteFlag <= 0) { map.put("flag", false); - map.put("msg", "search error"); + map.put("msg", "delete error"); return map; } map.put("flag", true); - map.put("msg", "update success"); + map.put("msg", "delete success"); + System.out.println("deleteS------------"); return map; } - @RequestMapping(value = "/addShop", method = RequestMethod.POST) - public Map addProduct(@RequestBody String userId,@RequestBody String productId,@RequestBody String num) { - Integer addFlag = shoppingCartService.addShoppingCart(Integer.valueOf(userId),Integer.valueOf(productId),Integer.valueOf(num)); + + @RequestMapping(value = "/update", method = RequestMethod.POST) + public Map updateProduct(@RequestBody CatShoppingCart catProduct) { + Integer updateFlag = shoppingCartService.updateShoppingCart(catProduct); Map map = new HashMap<>(); - if (addFlag <= 0) { + if (updateFlag <= 0) { map.put("flag", false); - map.put("msg", "add error"); + map.put("msg", "search error"); return map; } map.put("flag", true); - map.put("msg", "add success"); + map.put("msg", "update success"); return map; } diff --git a/CatApp_android_End/src/main/java/com/example/cat/mapper/ShoppingCartMapper.java b/CatApp_android_End/src/main/java/com/example/cat/mapper/ShoppingCartMapper.java index 87a1934..b713e68 100644 --- a/CatApp_android_End/src/main/java/com/example/cat/mapper/ShoppingCartMapper.java +++ b/CatApp_android_End/src/main/java/com/example/cat/mapper/ShoppingCartMapper.java @@ -28,11 +28,21 @@ public interface ShoppingCartMapper { */ List selectShoppingCart(@Param("userId") Integer userId); - Integer updateShoppingCart(@Param("num") Integer num,@Param("id")Integer id); - Integer deleteShoppingCart(@Param("id")Integer id); +// +// /** +// * 当数量为0时删除 +// * @param +// * @return +// */ +// Integer deleteShoppingCart(CatShoppingCart catShoppingCart); +// +// Integer addShoppingCart(@Param("userId")Integer userId,@Param("productId")Integer productId,@Param("num")Integer num); - Integer addShoppingCart(@Param("userId")Integer userId,@Param("productId")Integer productId,@Param("num")Integer num); + Integer deleteShop(@Param("userId")Integer userId,@Param("productId")Integer productId); + Integer addShop(@Param("userId")Integer userId,@Param("productId")Integer productId); + + Integer updateShoppingCart(CatShoppingCart catShoppingCart); } diff --git a/CatApp_android_End/src/main/java/com/example/cat/service/ShoppingCartService.java b/CatApp_android_End/src/main/java/com/example/cat/service/ShoppingCartService.java index 311207e..81c8456 100644 --- a/CatApp_android_End/src/main/java/com/example/cat/service/ShoppingCartService.java +++ b/CatApp_android_End/src/main/java/com/example/cat/service/ShoppingCartService.java @@ -36,17 +36,27 @@ public class ShoppingCartService { public List selectShoppingCart(Integer userId) { return shoppingCartMapper.selectShoppingCart(userId); } - public Integer updateShoppingCart(Integer num, Integer id) { - return shoppingCartMapper.updateShoppingCart(num,id); +// public Integer updateShoppingCart(Integer num, Integer productId,Integer userId) { +// return shoppingCartMapper.updateShoppingCart(num,productId,userId); +// } +// +// public Integer deleteShoppingCart(CatShoppingCart catShoppingCart) { +// return shoppingCartMapper.deleteShoppingCart(catShoppingCart); +// } +// +// public Integer addShoppingCart(Integer userId,Integer productId,Integer num) { +// return shoppingCartMapper.addShoppingCart(userId,productId,num); + //} + + public Integer deleteShop(Integer userId,Integer productId){ + return shoppingCartMapper.deleteShop(userId,productId); } - - public Integer deleteShoppingCart(Integer id) { - return shoppingCartMapper.deleteShoppingCart(id); + public Integer addShop(Integer userId,Integer productId){ + return shoppingCartMapper.addShop(userId,productId); } - public Integer addShoppingCart(Integer userId,Integer productId,Integer num) { - return shoppingCartMapper.addShoppingCart(userId,productId,num); + public Integer updateShoppingCart(CatShoppingCart catShoppingCart){ + return shoppingCartMapper.updateShoppingCart(catShoppingCart); } - } diff --git a/CatApp_android_End/src/main/resources/mapper/ShoppingCartMapper.xml b/CatApp_android_End/src/main/resources/mapper/ShoppingCartMapper.xml index c4345a7..8a454f9 100644 --- a/CatApp_android_End/src/main/resources/mapper/ShoppingCartMapper.xml +++ b/CatApp_android_End/src/main/resources/mapper/ShoppingCartMapper.xml @@ -8,23 +8,25 @@ - - update orange_shoppingcart set - - num=#{num}, - - where id=#{id} + + update orange_shoppingcart set num=#{num} + where product_id=#{productId} and user_id=userId + - delete from orange_shoppingcart where id=#{id} and num=0; + delete from orange_shoppingcart where num=0; + - delete from orange_shoppingcart where id=#{id} ; + delete from orange_shoppingcart where product_id=#{productId} and user_id=#{userId} ; - - insert into orange_shoppingcart(user_id,product_id,num) values (#{user_id},#{product_id},#{num}) + + + insert into orange_shoppingcart(user_id,product_id,num) values (#{userId},#{productId},#{num}) + - insert into orange_shoppingcart(user_id,product_id,num) values (#{user_id},#{product_id},#{num}) + insert into orange_shoppingcart(user_id,product_id,num) values (#{userId},#{productId},1) + \ No newline at end of file diff --git a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/activity/IndexActivity.java b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/activity/IndexActivity.java index 573bbb8..5f99466 100644 --- a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/activity/IndexActivity.java +++ b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/activity/IndexActivity.java @@ -30,7 +30,7 @@ public class IndexActivity extends Activity implements View.OnClickListener { private ShoppingCartFragment shoppingCartFragment; private PearsonFragment pearsonFragment; private LinearLayout indexLine, productLine, shoppingCartLine, pearsonLine; - public static List cartList = new ArrayList<>(); + public static List cartList = new ArrayList<>(); private Integer userId=1; @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -107,6 +107,9 @@ public class IndexActivity extends Activity implements View.OnClickListener { FragmentTransaction transaction = getFragmentManager().beginTransaction(); if (productFragment == null) { productFragment = new ProductFragment(); + Intent intent = IndexActivity.this.getIntent(); + Bundle bundle = intent.getExtras(); + productFragment.setArguments(bundle); } transaction.replace(R.id.main_content0, productFragment); transaction.commit(); @@ -167,5 +170,4 @@ public class IndexActivity extends Activity implements View.OnClickListener { } }).start(); } - } diff --git a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/adapter/ShopAdapter.java b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/adapter/ShopAdapter.java index 7ee1210..3bb1c37 100644 --- a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/adapter/ShopAdapter.java +++ b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/adapter/ShopAdapter.java @@ -1,5 +1,6 @@ package com.example.catapp.adapter; +import android.annotation.SuppressLint; import android.app.FragmentTransaction; import android.content.Context; import android.util.Log; @@ -41,22 +42,24 @@ public class ShopAdapter extends RecyclerView.Adapter { } } - public ShopAdapter(Context context,List catProductList, List shoppingCartList){ + public ShopAdapter(Context context, List catProductList, List shoppingCartList){ this.catProductList = catProductList; this.shoppingCartList=shoppingCartList; this.context=context; + } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view= View.inflate(context,R.layout.item,null); final ViewHolder holder=new ViewHolder(view); + holder.leftImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position=holder.getAdapterPosition(); ShoppingCartPack shoppingCartPack=shoppingCartList.get(position); - Toast.makeText(view.getContext(), "click-"+shoppingCartPack.getNum(), Toast.LENGTH_SHORT).show(); + Toast.makeText(view.getContext(), "click-"+shoppingCartPack.getNum(), Toast.LENGTH_SHORT).show(); } }); holder.rightImage.setOnClickListener(new View.OnClickListener() { @@ -91,16 +94,19 @@ public class ShopAdapter extends RecyclerView.Adapter { public int getItemCount() { return shoppingCartList.size(); } -// public interface OnItemClickListener { -// -// public void OnItemClick(View view, ShoppingCartPack data ); -// } -// -// //需要外部访问,所以需要设置set方法,方便调用 -// private OnItemClickListener onItemClickListener; -// -// public void setOnItemClickListener(OnItemClickListener onItemClickListener) { -// this.onItemClickListener = onItemClickListener; -// } + public void notifyData(List poiItemList,List ProductList) { + if (poiItemList != null) { + int previousSize = shoppingCartList.size(); + shoppingCartList.clear(); + shoppingCartList.addAll(poiItemList); + } + if (ProductList != null) { + int previousSize = catProductList.size(); + catProductList.clear(); + notifyItemRangeRemoved(0, previousSize); + catProductList.addAll(ProductList); + notifyItemRangeInserted(0, ProductList.size()); + } + } } diff --git a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/adapter/ShoppingCartAdapter.java b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/adapter/ShoppingCartAdapter.java index b5b81bc..37b8ece 100644 --- a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/adapter/ShoppingCartAdapter.java +++ b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/adapter/ShoppingCartAdapter.java @@ -42,7 +42,7 @@ public class ShoppingCartAdapter extends BaseAdapter { public View getView(int position, View convertView, ViewGroup parent) { ShoppingCartAdapter.ViewHolder viewHolder; if (convertView == null) { - convertView = layoutInflater.inflate(R.layout.shoppingcart_detail_content, null); + convertView = layoutInflater.inflate(R.layout.item, null); viewHolder = new ShoppingCartAdapter.ViewHolder(); viewHolder.productImage = convertView.findViewById(R.id.category_product_image); viewHolder.productName = convertView.findViewById(R.id.category_product_name); @@ -66,6 +66,8 @@ public class ShoppingCartAdapter extends BaseAdapter { class ViewHolder { ImageView productImage; TextView productName, productPrice,num; + ImageView left,right; + } } diff --git a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/entity/ShoppingCartPack.java b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/entity/ShoppingCartPack.java index c43c96a..5fddd84 100644 --- a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/entity/ShoppingCartPack.java +++ b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/entity/ShoppingCartPack.java @@ -10,7 +10,7 @@ public class ShoppingCartPack { this.id = id; } - public Integer getUserId() { + public Integer getUserId(int userid) { return userId; } diff --git a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/IndexFragment.java b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/IndexFragment.java index 12e7578..66f38ff 100644 --- a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/IndexFragment.java +++ b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/IndexFragment.java @@ -156,4 +156,5 @@ public class IndexFragment extends Fragment implements View.OnClickListener { } } + } diff --git a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ProductDetailFragment.java b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ProductDetailFragment.java new file mode 100644 index 0000000..a65df43 --- /dev/null +++ b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ProductDetailFragment.java @@ -0,0 +1,85 @@ +package com.example.catapp.fragment; + +import android.app.Fragment; +import android.app.FragmentTransaction; +import android.os.Bundle; +import android.os.Message; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + + +import com.example.catapp.R; +import com.example.catapp.activity.IndexActivity; +import com.example.catapp.adapter.ShopAdapter; +import com.example.catapp.entity.ShoppingCartPack; +import com.example.catapp.netrequest.OkHttpShoppingcart; + +import java.io.IOException; + +public class ProductDetailFragment extends Fragment implements View.OnClickListener{ + ImageView imageView; + TextView textView1,textView2; + Button buttonAdd,buttonSale; + LinearLayout line1,line2,line3; + int p; + int userid; + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + View view = LayoutInflater.from(getActivity()).inflate(R.layout.good_detail, container, false); + init(view); + return view; + } + private void init(View view){ + imageView=view.findViewById(R.id.category_product_image); + textView1=view.findViewById(R.id.category_product_name); + textView2=view.findViewById(R.id.category_product_price); + buttonAdd=view.findViewById(R.id.buttonA); + buttonAdd.setOnClickListener(this); + buttonSale=view.findViewById(R.id.buttonS); + line1=view.findViewById(R.id.li1); + line2=view.findViewById(R.id.li2); + line3=view.findViewById(R.id.li3); + setData(); + } + private void setData() { + Bundle bundle = getArguments(); + imageView.setImageBitmap(bundle.getParcelable("imgBitmap")); + textView1.setText(bundle.getString("name")); + textView2.setText(bundle.getString("price")); + p=bundle.getInt("productId"); + userid=bundle.getInt("userId"); + Log.d("p","u"+userid); + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.buttonA: + new Thread(new Runnable() { + @Override + public void run() { + OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart(); + try { + okHttpShoppingcart.addShoppingCart(userid,p); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + }).start(); + } + } + + +} diff --git a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ProductFragment.java b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ProductFragment.java index f12a151..0275dee 100644 --- a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ProductFragment.java +++ b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ProductFragment.java @@ -1,6 +1,7 @@ package com.example.catapp.fragment; import android.app.Fragment; +import android.app.FragmentTransaction; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; @@ -10,6 +11,7 @@ import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; import android.widget.GridView; import android.widget.Spinner; import android.widget.Toast; @@ -26,6 +28,7 @@ import com.example.catapp.entity.CatProductPack; import com.example.catapp.entity.CatUser; import com.example.catapp.entity.Condition; import com.example.catapp.netrequest.OkHttpClientProduct; +import com.example.catapp.netrequest.OkHttpShoppingcart; import com.example.catapp.netrequest.OkHttpUser; import java.io.IOException; @@ -38,8 +41,10 @@ public class ProductFragment extends Fragment { private ListViewAdapter listViewAdapter; private List conditionList; private GridView productGridView; - private List catProductList = new ArrayList<>(); + public static List catProductList = new ArrayList<>(); Handler h=null; + ProductDetailFragment productDetailFragment; + Bundle bundle=new Bundle(); @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -96,9 +101,43 @@ public class ProductFragment extends Fragment { public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); ProductAdapter productAdapter = new ProductAdapter(getActivity(), catProductList); - System.out.println(catProductList.size()); - System.out.println(catProductList.size()); productGridView.setAdapter(productAdapter); + productGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { +// FragmentTransaction transaction = getFragmentManager().beginTransaction(); +// if (productDetailFragment == null) { +// bundle = getArguments(); +// int i=bundle.getInt("id"); +// bundle.putInt("userId",i); +// bundle.putInt("productId",catProductList.get(position).getId()); +// bundle.putString("name",catProductList.get(position).getName()); +// bundle.putString("price",catProductList.get(position).getPrice().toString()); +// bundle.putParcelable("imgBitmap",catProductList.get(position).getImgBitmap()); +// productDetailFragment = new ProductDetailFragment(); +// productDetailFragment.setArguments(bundle); +// } +// transaction.replace(R.id.main_content0,productDetailFragment); +// transaction.commit(); + bundle = getArguments(); + int i=bundle.getInt("id"); + int p =catProductList.get(position).getId(); + Toast.makeText(getContext(), "id"+catProductList.get(position).getId()+"加入购物车", Toast.LENGTH_SHORT).show(); + new Thread(new Runnable() { + @Override + public void run() { + OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart(); + try { + okHttpShoppingcart.addShoppingCart(i,p); + //okHttpShoppingcart.deleteShoppingCart(i,p); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + }).start(); + } + }); } }; @@ -108,7 +147,6 @@ public class ProductFragment extends Fragment { OkHttpClientProduct okHttpClientProduct=new OkHttpClientProduct(); try { catProductList=okHttpClientProduct.getProductPack(); - } catch (IOException e) { throw new RuntimeException(e); } diff --git a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ShoppingCartFragment.java b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ShoppingCartFragment.java index 9046051..6c5c4d2 100644 --- a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ShoppingCartFragment.java +++ b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/fragment/ShoppingCartFragment.java @@ -1,5 +1,7 @@ package com.example.catapp.fragment; +import android.animation.Animator; +import android.annotation.SuppressLint; import android.app.Fragment; import android.app.FragmentTransaction; import android.os.Bundle; @@ -7,13 +9,17 @@ import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.LayoutInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.view.animation.Animation; import android.widget.AdapterView; import android.widget.Button; +import android.widget.CheckBox; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -41,24 +47,23 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen private ProductFragment productFragment; private GridView productGridView; private TextView usernameText; + ImageView left_l,right_r; private RecyclerView recyclerView; private Integer userId=13; private List catProductList= new ArrayList<>(); Handler h=null; - ShopAdapter shopAdapter; + public ShopAdapter shopAdapter=null; + static int o; + private boolean isGetData = false; + CheckBox checkBox; + + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //给购物车集合赋值,用以判断界面的展示 cartList=IndexActivity.cartList; Log.d("ShoppingCartFragment onCrate init cartList",""+cartList); - - } - - @Override - public void onActivityCreated(@Nullable Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - //LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this); } @Nullable @@ -66,10 +71,10 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view; cartList=IndexActivity.cartList; + Log.d("cartList---------",""+cartList.size()); if (cartList.size() > 0) { //有商品 view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_have_product, container, false); - linkHttp(view); } else { view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_no_product, container, false); @@ -83,7 +88,8 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen */ private void init(View view) { usernameText=view.findViewById(R.id.username); - //productGridView = view.findViewById(R.id.cart_productList); + checkBox=view.findViewById(R.id.checkbox); + productGridView = view.findViewById(R.id.cart_productList); if (cartList.size() > 0) { } @@ -91,7 +97,6 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen walkButton = view.findViewById(R.id.random_search); walkButton.setOnClickListener(this); } - setData(); } private void setData() { @@ -100,25 +105,28 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen userId=(Integer)bundle.getInt("id"); System.out.println(userId); } - @Override - public void onClick(View v) { - if (cartList.size() > 0) { - - } - else { - switch (v.getId()) { - case R.id.random_search: - //开启事务,fragment的控制是由事务来实现的 - FragmentTransaction transaction = getFragmentManager().beginTransaction(); - if (productFragment == null) { - productFragment = new ProductFragment(); - } - transaction.replace(R.id.main_content0, productFragment); - transaction.commit(); - break; - } - } - } +// @Override +// public void onClick(View v) { +// if (cartList.size() > 0) { +// +// } +// else { +// switch (v.getId()) { +// case R.id.random_search: +// //开启事务,fragment的控制是由事务来实现的 +// FragmentTransaction transaction = getFragmentManager().beginTransaction(); +// if (productFragment == null) { +// Bundle bundle = getArguments(); +// productFragment.setArguments(bundle); +// productFragment = new ProductFragment(); +// bundle.putInt("id",userId); +// } +// transaction.replace(R.id.main_content0, productFragment); +// transaction.commit(); +// break; +// } +// } +// } /** @@ -129,11 +137,40 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); - recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false)); - shopAdapter=new ShopAdapter(getActivity(),catProductList,cartList); - recyclerView.setAdapter(shopAdapter); - //ShoppingCartAdapter shoppingCartAdapter = new ShoppingCartAdapter(getActivity(), catProductList,cartList); - //productGridView.setAdapter(shoppingCartAdapter); + +// if(shopAdapter!=null) +// shopAdapter.notifyData(cartList,catProductList); +// recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false)); +// shopAdapter=new ShopAdapter(getActivity(),catProductList,cartList); +// recyclerView.setAdapter(shopAdapter); + ShoppingCartAdapter shoppingCartAdapter=new ShoppingCartAdapter(getActivity(),catProductList,cartList); + productGridView.setAdapter(shoppingCartAdapter); + + productGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + Bundle bundle = getArguments(); + //usernameText.setText(String.format("用户名:%s", bundle.getString("username"))); + int i=(Integer)bundle.getInt("id"); + int p=cartList.get(position).getId(); + System.out.println(""+1111111+"----"+p+"---"+i); + new Thread(new Runnable() { + @SuppressLint("HandlerLeak") + @Override + public void run() { + OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart(); + try { + okHttpShoppingcart.deleteShoppingCart(i,p); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + }).start(); + Toast.makeText(getContext(), "delete", Toast.LENGTH_SHORT).show(); + } + }); + } }; @@ -142,8 +179,10 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen public void run() { OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart(); try { - recyclerView=view.findViewById(R.id.recyclerview); - //productGridView = view.findViewById(R.id.cart_productList); +// recyclerView=view.findViewById(R.id.recyclerview); + productGridView = view.findViewById(R.id.cart_productList); + left_l=view.findViewById(R.id.left); + right_r=view.findViewById(R.id.right); catProductList= okHttpShoppingcart.getProductPack(userId); cartList=okHttpShoppingcart.getShoppingCartPack(userId); } catch (IOException e) { @@ -154,4 +193,10 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen } }).start(); } + + + @Override + public void onClick(View v) { + + } } diff --git a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/netrequest/OkHttpShoppingcart.java b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/netrequest/OkHttpShoppingcart.java index b26d2e1..3a4a1c4 100644 --- a/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/netrequest/OkHttpShoppingcart.java +++ b/CatApp_android_frontEnd/app/src/main/java/com/example/catapp/netrequest/OkHttpShoppingcart.java @@ -17,6 +17,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import okhttp3.FormBody; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -40,7 +41,7 @@ public class OkHttpShoppingcart { RequestBody requestBody = RequestBody.create(jsonStr, mediaType); Request request = new Request.Builder() - .url(OkHttpClientProduct.Url+"/orange/shoppingcart/search1") + .url(OkHttpClientProduct.Url+"/orange/shopping/search1") .post(requestBody) .build(); Response response = okHttpClient.newCall(request).execute(); @@ -53,6 +54,58 @@ public class OkHttpShoppingcart { return list; } + + public void addShoppingCart(int u,int p) throws IOException { + OkHttpClient okHttpClient = new OkHttpClient(); + //数据类型为json格式 + MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); + //将对象转为JSON字符串 + String jsonStr = JSONObject.toJSONString(u); + String jsonStr1 = JSONObject.toJSONString(p); + RequestBody requestBody=new FormBody.Builder() + .add("userId", jsonStr) + .add("productId", jsonStr1) + .build(); + Request request = new Request.Builder() + .url(OkHttpClientProduct.Url+"/orange/shopping/addS") + .post(requestBody) + .build(); + try { + Response response = okHttpClient.newCall(request).execute(); + Log.d("addS"," "+response.isSuccessful()); + JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(response.body()).string()); + }catch (IOException e){ + + } + + } + + + public void deleteShoppingCart(int userId,int productId) throws IOException { + OkHttpClient okHttpClient = new OkHttpClient(); + //数据类型为json格式 + MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); + //将对象转为JSON字符串 + String jsonStr = JSONObject.toJSONString(userId); + String jsonStr1 = JSONObject.toJSONString(productId); + RequestBody requestBody=new FormBody.Builder() + .add("userId", jsonStr) + .add("productId", jsonStr1) + .build(); + Request request = new Request.Builder() + .url(OkHttpClientProduct.Url+"/orange/shopping/deleteS") + .post(requestBody) + .build(); + try { + Response response = okHttpClient.newCall(request).execute(); + Log.d("deleteS"," "+response.isSuccessful()); + JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(response.body()).string()); + }catch (IOException e){ + + } + + } + /** * 获取当前登录用户的购物车信息 * @param userId 用户id @@ -68,7 +121,7 @@ public class OkHttpShoppingcart { String jsonStr = JSONObject.toJSONString(userId); RequestBody requestBody = RequestBody.create(jsonStr, mediaType); Request request = new Request.Builder() - .url(OkHttpClientProduct.Url+"/orange/shoppingcart/sharecropping") + .url(OkHttpClientProduct.Url+"/orange/shopping/sharecropping") .post(requestBody) .build(); Response response = okHttpClient.newCall(request).execute(); diff --git a/CatApp_android_frontEnd/app/src/main/res/layout/cart_buttonlayout.xml b/CatApp_android_frontEnd/app/src/main/res/layout/cart_buttonlayout.xml new file mode 100644 index 0000000..9e7494e --- /dev/null +++ b/CatApp_android_frontEnd/app/src/main/res/layout/cart_buttonlayout.xml @@ -0,0 +1,46 @@ + + + + + + + +