购物车添加商品和删除

master
ghtMare 2 years ago
parent 622cd6a086
commit 0d662c0ad6

@ -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<String, Object> deleteShoppingCart(@RequestBody String id) {
Integer deleteFlag = shoppingCartService.deleteShoppingCart(Integer.valueOf(id));
@RequestMapping(value = "/addS", method = RequestMethod.POST)
public Map<String, Object> addProduct(@Param("userId") String userId,@Param("productId") String productId) {
Integer addFlag = shoppingCartService.addShop(Integer.valueOf(userId),Integer.valueOf(productId));
Map<String, Object> 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<String, Object> 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<String, Object> deleteProduct(@Param("userId") String userId,@Param("productId") String productId) {
Integer deleteFlag = shoppingCartService.deleteShop(Integer.valueOf(userId),Integer.valueOf(productId));
Map<String, Object> 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<String, Object> 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<String, Object> updateProduct(@RequestBody CatShoppingCart catProduct) {
Integer updateFlag = shoppingCartService.updateShoppingCart(catProduct);
Map<String, Object> 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;
}

@ -28,11 +28,21 @@ public interface ShoppingCartMapper {
*/
List<CatShoppingCart> 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);
}

@ -36,17 +36,27 @@ public class ShoppingCartService {
public List<CatShoppingCart> 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);
}
}

@ -8,23 +8,25 @@
<select id="selectShoppingCart" resultType="com.example.cat.entity.CatShoppingCart">
select id,user_id,product_id,num from orange_shoppingcart where user_id=#{userId}
</select>
<update id="updateShoppingCart">
update orange_shoppingcart set
<trim suffixOverrides=",">
num=#{num},
</trim>
where id=#{id}
<update id="updateShoppingCart" parameterType="com.example.cat.entity.CatShoppingCart">
update orange_shoppingcart set num=#{num}
where product_id=#{productId} and user_id=userId
</update>
<delete id="deleteShoppingCart">
delete from orange_shoppingcart where id=#{id} and num=0;
delete from orange_shoppingcart where num=0;
</delete>
<delete id="deleteShop">
delete from orange_shoppingcart where id=#{id} ;
delete from orange_shoppingcart where product_id=#{productId} and user_id=#{userId} ;
</delete>
<insert id="addShoppingCart" parameterType="com.example.cat.entity.CatShoppingCart">
insert into orange_shoppingcart(user_id,product_id,num) values (#{user_id},#{product_id},#{num})
<insert id="addShoppingCart" >
insert into orange_shoppingcart(user_id,product_id,num) values (#{userId},#{productId},#{num})
</insert>
<insert id="addShop" parameterType="com.example.cat.entity.CatShoppingCart">
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)
</insert>
</mapper>

@ -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<ShoppingCartPack> cartList = new ArrayList<>();
public static List<ShoppingCartPack> 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();
}
}

@ -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<ShopAdapter.ViewHolder> {
}
}
public ShopAdapter(Context context,List<CatProductPack> catProductList, List<ShoppingCartPack> shoppingCartList){
public ShopAdapter(Context context, List<CatProductPack> catProductList, List<ShoppingCartPack> 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<ShopAdapter.ViewHolder> {
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<ShoppingCartPack> poiItemList,List<CatProductPack> 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());
}
}
}

@ -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;
}
}

@ -10,7 +10,7 @@ public class ShoppingCartPack {
this.id = id;
}
public Integer getUserId() {
public Integer getUserId(int userid) {
return userId;
}

@ -156,4 +156,5 @@ public class IndexFragment extends Fragment implements View.OnClickListener {
}
}
}

@ -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();
}
}
}

@ -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<Condition> conditionList;
private GridView productGridView;
private List<CatProductPack> catProductList = new ArrayList<>();
public static List<CatProductPack> 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);
}

@ -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<CatProductPack> 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) {
}
}

@ -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();

@ -0,0 +1,46 @@
<?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="match_parent">
<!-- 结算区域 -->
<RelativeLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#fafafa"
android:padding="6dp">
<TextView
android:id="@+id/total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="总计0.00"
android:textColor="#333"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="@+id/checkout"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#f44336"
android:text="结算"
android:textColor="#fff" />
<Button
android:id="@+id/clear_cart"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_toLeftOf="@id/checkout"
android:layout_centerVertical="true"
android:background="#333"
android:text="清空购物车"
android:textColor="#fff" />
</RelativeLayout>
</LinearLayout>

@ -6,9 +6,25 @@
android:orientation="vertical">
<include layout="@layout/shoppingcart_title" />
<!--有商品时的布局-->
<include layout="@layout/shop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="627dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<GridView
android:id="@+id/cart_productList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="10dp"
android:numColumns="1"
android:verticalSpacing="10dp" />
</LinearLayout>
<include layout="@layout/cart_buttonlayout" />
</LinearLayout>

@ -0,0 +1,82 @@
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/li1"
android:layout_width="match_parent"
android:layout_height="139dp"
android:background="#ffffff"
android:orientation="horizontal">
<ImageView
android:id="@+id/category_product_image"
android:layout_width="70dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="5dp"
tools:srcCompat="@tools:sample/avatars" />
<LinearLayout
android:id="@+id/li2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="6"
android:background="#1EFFFFFF"
android:orientation="vertical">
<TextView
android:id="@+id/category_product_name"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="20dp"
android:text="商品名称"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/category_product_price"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:text="价格" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/li3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/buttonA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/button_login"
android:text="加入购物车"
android:textColor="#fff"
android:textSize="18sp"
/>
<Button
android:id="@+id/buttonS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginLeft="40dp"
android:background="@drawable/button_login"
android:text="购买"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>

@ -1,70 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#14FFEB3B"
android:orientation="vertical"
>
<ImageView
android:id="@+id/category_product_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="139dp"
android:orientation="horizontal">
<TextView
android:id="@+id/category_product_name"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toRightOf="@+id/category_product_image"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:textColor="#050505"
android:textSize="16sp" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="46dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:id="@+id/category_product_price"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toRightOf="@+id/category_product_name"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:layout_marginTop="25dp"
android:gravity="center"
android:textColor="#050505"
android:textSize="16sp" />
<ImageView
android:id="@+id/left"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="55dp"
android:layout_toRightOf="@id/category_product_price"
android:background="@drawable/left"
<ImageView
android:id="@+id/category_product_image"
android:layout_width="111dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1" />
/>
<TextView
android:id="@+id/num"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_toRightOf="@+id/left"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:gravity="center"
android:background="#00FEFEFE"
android:textColor="#050505"
android:textSize="16sp" />
<ImageView
android:id="@+id/right"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_toRightOf="@id/num"
android:background="@drawable/right"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="6"
android:background="#1EFFFFFF"
android:orientation="vertical">
</RelativeLayout>
<TextView
android:id="@+id/category_product_name"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="20dp"
android:text="商品名称"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/category_product_price"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:text="价格" />
</LinearLayout>
<ImageView
android:id="@+id/left"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="100dp"
android:layout_toRightOf="@id/category_product_price"
android:background="@drawable/left" />
<TextView
android:id="@+id/num"
android:layout_width="18dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_toRightOf="@+id/left"
android:background="#00FEFEFE"
android:gravity="center"
android:textColor="#050505"
android:textSize="16sp" />
<ImageView
android:id="@+id/right"
android:layout_width="42dp"
android:layout_height="44dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:layout_toRightOf="@id/num"
android:background="@drawable/right" />
</LinearLayout>
</LinearLayout>

@ -1,11 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#14FFEB3B"
android:orientation="vertical"
android:focusable="false"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/category_product_image"
android:layout_width="80dp"
@ -70,5 +74,5 @@
android:text="+"
android:textColor="#1C1C1C"
android:textSize="18sp"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>

@ -11,7 +11,7 @@
Target Server Version : 80033
File Encoding : 65001
Date: 12/05/2023 22:43:53
Date: 14/05/2023 21:24:42
*/
SET NAMES utf8mb4;
@ -27,7 +27,7 @@ CREATE TABLE `orange_message` (
`send_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`used` enum('YES','NO') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'NO',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 36 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
) ENGINE = InnoDB AUTO_INCREMENT = 37 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of orange_message
@ -63,14 +63,14 @@ CREATE TABLE `orange_product` (
-- ----------------------------
-- Records of orange_product
-- ----------------------------
INSERT INTO `orange_product` VALUES (1, '猫粮', 'http://10.21.120.101:8081/uploaded/maoliang.png', 29.99, '2023-05-02 16:37:46');
INSERT INTO `orange_product` VALUES (2, '猫玩具', 'http://10.21.120.101:8081/uploaded/maowanju.png', 9.99, '2023-05-02 16:39:28');
INSERT INTO `orange_product` VALUES (3, '猫窝', 'http://10.21.120.101:8081/uploaded/maowo.png', 99.00, '2023-05-02 16:40:50');
INSERT INTO `orange_product` VALUES (4, '猫粮', 'http://10.21.120.101:8081/uploaded/ml.png', 36.00, '2023-05-02 16:41:54');
INSERT INTO `orange_product` VALUES (5, '猫粮盆', 'http://10.21.120.101:8081/uploaded/mlp.png', 9.00, '2023-05-02 16:42:54');
INSERT INTO `orange_product` VALUES (6, '香梨', 'http://10.21.120.101:8081/uploaded/li.png', 9.99, '2019-12-16 18:19:09');
INSERT INTO `orange_product` VALUES (7, '橙子', 'http://10.21.120.101:8081/uploaded/orange.png', 222.00, '2019-12-17 02:29:50');
INSERT INTO `orange_product` VALUES (8, '苹果', 'http://10.21.120.101:8081/uploaded/apple.png', 19.90, '2019-12-16 15:58:05');
INSERT INTO `orange_product` VALUES (1, '猫粮', 'http://10.21.91.105:8081/uploaded/maoliang.png', 29.99, '2023-05-02 16:37:46');
INSERT INTO `orange_product` VALUES (2, '猫玩具', 'http://10.21.91.105:8081/uploaded/maowanju.png', 9.99, '2023-05-02 16:39:28');
INSERT INTO `orange_product` VALUES (3, '猫窝', 'http://10.21.91.105:8081/uploaded/maowo.png', 99.00, '2023-05-02 16:40:50');
INSERT INTO `orange_product` VALUES (4, '猫粮', 'http://10.21.91.105:8081/uploaded/ml.png', 36.00, '2023-05-02 16:41:54');
INSERT INTO `orange_product` VALUES (5, '猫粮盆', 'http://10.21.91.105:8081/uploaded/mlp.png', 9.00, '2023-05-02 16:42:54');
INSERT INTO `orange_product` VALUES (6, '香梨', 'http://10.21.91.105:8081/uploaded/li.png', 9.99, '2019-12-16 18:19:09');
INSERT INTO `orange_product` VALUES (7, '橙子', 'http://10.21.91.105:8081/uploaded/orange.png', 222.00, '2019-12-17 02:29:50');
INSERT INTO `orange_product` VALUES (8, '苹果', 'http://10.21.91.105:8081/uploaded/apple.png', 19.90, '2019-12-16 15:58:05');
-- ----------------------------
-- Table structure for orange_sale
@ -97,16 +97,19 @@ CREATE TABLE `orange_shoppingcart` (
`product_id` int(0) NOT NULL DEFAULT 0 COMMENT '商品id',
`num` int(0) NOT NULL DEFAULT 0 COMMENT '数量',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of orange_shoppingcart
-- ----------------------------
INSERT INTO `orange_shoppingcart` VALUES (1, 1, 1, 1);
INSERT INTO `orange_shoppingcart` VALUES (2, 1, 2, 3);
INSERT INTO `orange_shoppingcart` VALUES (3, 5, 3, 0);
INSERT INTO `orange_shoppingcart` VALUES (4, 13, 3, 5);
INSERT INTO `orange_shoppingcart` VALUES (5, 13, 4, 2);
INSERT INTO `orange_shoppingcart` VALUES (6, 2, 2, 2);
INSERT INTO `orange_shoppingcart` VALUES (7, 13, 8, 4);
INSERT INTO `orange_shoppingcart` VALUES (30, 13, 5, 1);
INSERT INTO `orange_shoppingcart` VALUES (36, 13, 1, 1);
INSERT INTO `orange_shoppingcart` VALUES (37, 13, 2, 1);
INSERT INTO `orange_shoppingcart` VALUES (39, 13, 3, 1);
-- ----------------------------
-- Table structure for orange_user
@ -119,7 +122,7 @@ CREATE TABLE `orange_user` (
`sex` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
`city` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 13 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
) ENGINE = InnoDB AUTO_INCREMENT = 15 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of orange_user
@ -135,5 +138,6 @@ INSERT INTO `orange_user` VALUES (8, '1', '1', '男', '江西省 赣州市 章
INSERT INTO `orange_user` VALUES (9, '1', '1', '', '江西省 赣州市 章贡区');
INSERT INTO `orange_user` VALUES (11, 'admin', '1', '', '江西省 赣州市 章贡区');
INSERT INTO `orange_user` VALUES (13, '2', '2', '', '江西省 赣州市 章贡区');
INSERT INTO `orange_user` VALUES (14, '3', '3', '', '江西省 赣州市 章贡区');
SET FOREIGN_KEY_CHECKS = 1;

Loading…
Cancel
Save