修改适配器

master
ghtMare 2 years ago
parent b0e2420b7f
commit 622cd6a086

@ -97,4 +97,45 @@ public class CatShoppingCartController {
} }
} }
@RequestMapping(value = "/deleteShop", method = RequestMethod.POST)
public Map<String, Object> deleteShoppingCart(@RequestBody String id) {
Integer deleteFlag = shoppingCartService.deleteShoppingCart(Integer.valueOf(id));
Map<String, Object> map = new HashMap<>();
if (deleteFlag <= 0) {
map.put("flag", false);
map.put("msg", "delete error");
return map;
}
map.put("flag", true);
map.put("msg", "delete success");
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));
Map<String, Object> map = new HashMap<>();
if (updateFlag <= 0) {
map.put("flag", false);
map.put("msg", "search error");
return map;
}
map.put("flag", true);
map.put("msg", "update success");
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));
Map<String, Object> map = new HashMap<>();
if (addFlag <= 0) {
map.put("flag", false);
map.put("msg", "add error");
return map;
}
map.put("flag", true);
map.put("msg", "add success");
return map;
}
} }

@ -28,5 +28,11 @@ public interface ShoppingCartMapper {
*/ */
List<CatShoppingCart> selectShoppingCart(@Param("userId") Integer userId); List<CatShoppingCart> selectShoppingCart(@Param("userId") Integer userId);
Integer updateShoppingCart(@Param("num") Integer num,@Param("id")Integer id);
Integer deleteShoppingCart(@Param("id")Integer id);
Integer addShoppingCart(@Param("userId")Integer userId,@Param("productId")Integer productId,@Param("num")Integer num);
} }

@ -3,6 +3,7 @@ package com.example.cat.service;
import com.example.cat.entity.CatProduct; import com.example.cat.entity.CatProduct;
import com.example.cat.entity.CatShoppingCart; import com.example.cat.entity.CatShoppingCart;
import com.example.cat.mapper.ShoppingCartMapper; import com.example.cat.mapper.ShoppingCartMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -35,5 +36,17 @@ public class ShoppingCartService {
public List<CatShoppingCart> selectShoppingCart(Integer userId) { public List<CatShoppingCart> selectShoppingCart(Integer userId) {
return shoppingCartMapper.selectShoppingCart(userId); return shoppingCartMapper.selectShoppingCart(userId);
} }
public Integer updateShoppingCart(Integer num, Integer id) {
return shoppingCartMapper.updateShoppingCart(num,id);
}
public Integer deleteShoppingCart(Integer id) {
return shoppingCartMapper.deleteShoppingCart(id);
}
public Integer addShoppingCart(Integer userId,Integer productId,Integer num) {
return shoppingCartMapper.addShoppingCart(userId,productId,num);
}
} }

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

@ -33,4 +33,5 @@ dependencies {
implementation 'liji.library.dev:citypickerview:1.1.0' implementation 'liji.library.dev:citypickerview:1.1.0'
implementation "com.squareup.okhttp3:okhttp:4.2.2" implementation "com.squareup.okhttp3:okhttp:4.2.2"
implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.12' implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.12'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
} }

@ -0,0 +1,106 @@
package com.example.catapp.adapter;
import android.app.FragmentTransaction;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.catapp.R;
import com.example.catapp.entity.CatProductPack;
import com.example.catapp.entity.ShoppingCartPack;
import com.example.catapp.fragment.ProductFragment;
import java.util.List;
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {
private List<ShoppingCartPack> shoppingCartList;
private List<CatProductPack> catProductList;
Context context;
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView productImage,leftImage,rightImage;
TextView productName, productPrice,num;
public ViewHolder(View view){
super(view);
productImage=(ImageView) view.findViewById(R.id.category_product_image);
productName=(TextView) view.findViewById(R.id.category_product_name);
productPrice=(TextView) view.findViewById(R.id.category_product_price);
num=(TextView) view.findViewById(R.id.num);
leftImage=(ImageView) view.findViewById(R.id.left);
rightImage=(ImageView) view.findViewById(R.id.right);
}
}
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();
}
});
holder.rightImage.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();
}
});
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
int i;
i=position;
ShoppingCartPack shoppingCartPack=shoppingCartList.get(i);
CatProductPack catProductPack=catProductList.get(i);
if(catProductPack!=null) {
holder.productImage.setImageBitmap(catProductPack.getImgBitmap());
holder.productName.setText(catProductPack.getName());
Log.d("hodler",""+catProductPack.getName());
holder.productPrice.setText(String.format("%s元", String.valueOf(catProductPack.getPrice())));
}
if(shoppingCartPack!=null)
holder.num.setText(""+shoppingCartPack.getNum());
}
@Override
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;
// }
}

@ -35,7 +35,6 @@ public class ShoppingCartAdapter extends BaseAdapter {
return catProductList.get(position); return catProductList.get(position);
} }
public long getItemId(int position) { public long getItemId(int position) {
return position; return position;
} }

@ -17,10 +17,13 @@ import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.catapp.R; import com.example.catapp.R;
import com.example.catapp.activity.IndexActivity; import com.example.catapp.activity.IndexActivity;
import com.example.catapp.adapter.ProductAdapter; import com.example.catapp.adapter.ProductAdapter;
import com.example.catapp.adapter.ShopAdapter;
import com.example.catapp.adapter.ShoppingCartAdapter; import com.example.catapp.adapter.ShoppingCartAdapter;
import com.example.catapp.entity.CatProductPack; import com.example.catapp.entity.CatProductPack;
import com.example.catapp.entity.ShoppingCart; import com.example.catapp.entity.ShoppingCart;
@ -38,11 +41,11 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
private ProductFragment productFragment; private ProductFragment productFragment;
private GridView productGridView; private GridView productGridView;
private TextView usernameText; private TextView usernameText;
Button left1; private RecyclerView recyclerView;
private Integer userId=13; private Integer userId=13;
private List<CatProductPack> catProductList= new ArrayList<>(); private List<CatProductPack> catProductList= new ArrayList<>();
Handler h=null; Handler h=null;
ShopAdapter shopAdapter;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -52,22 +55,11 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
} }
// @Override @Override
// public void onActivityCreated(@Nullable Bundle savedInstanceState) { public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
// productGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
// @Override }
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// FragmentTransaction transaction = getFragmentManager().beginTransaction();
// if (productFragment == null) {
// productFragment = new ProductFragment();
// }
// transaction.replace(R.id.main_content0, productFragment);
// transaction.commit();
//
// }
// });
// }
@Nullable @Nullable
@Override @Override
@ -77,6 +69,7 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
if (cartList.size() > 0) { if (cartList.size() > 0) {
//有商品 //有商品
view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_have_product, container, false); view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_have_product, container, false);
linkHttp(view); linkHttp(view);
} else { } else {
view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_no_product, container, false); view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_no_product, container, false);
@ -136,8 +129,11 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
@Override @Override
public void handleMessage(@NonNull Message msg) { public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg); super.handleMessage(msg);
ShoppingCartAdapter shoppingCartAdapter = new ShoppingCartAdapter(getActivity(), catProductList,cartList); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
productGridView.setAdapter(shoppingCartAdapter); shopAdapter=new ShopAdapter(getActivity(),catProductList,cartList);
recyclerView.setAdapter(shopAdapter);
//ShoppingCartAdapter shoppingCartAdapter = new ShoppingCartAdapter(getActivity(), catProductList,cartList);
//productGridView.setAdapter(shoppingCartAdapter);
} }
}; };
@ -146,7 +142,8 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
public void run() { public void run() {
OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart(); OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart();
try { try {
productGridView = view.findViewById(R.id.cart_productList); recyclerView=view.findViewById(R.id.recyclerview);
//productGridView = view.findViewById(R.id.cart_productList);
catProductList= okHttpShoppingcart.getProductPack(userId); catProductList= okHttpShoppingcart.getProductPack(userId);
cartList=okHttpShoppingcart.getShoppingCartPack(userId); cartList=okHttpShoppingcart.getShoppingCartPack(userId);
} catch (IOException e) { } catch (IOException e) {

@ -1,15 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<include layout="@layout/shoppingcart_title" /> <include layout="@layout/shoppingcart_title" />
<!--有商品时的布局--> <!--有商品时的布局-->
<GridView <include layout="@layout/shop" />
android:id="@+id/cart_productList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="1" />
</LinearLayout> </LinearLayout>

@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="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" />
<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" />
<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"
/>
<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"
/>
</RelativeLayout>

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Loading…
Cancel
Save