个人中心主要代码:
1 class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 2 @NonNull 3 @Override 4 public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 5 // View view = View.inflate(parent.getContext(), R.layout.adapter_recycler_home, null); 6 View view = View.inflate(parent.getContext(), R.layout.item_favour, null); 7 MyViewHolder myViewHolder = new MyViewHolder(view); 8 return myViewHolder; 9 } 10 11 @Override 12 public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder2, int position) { 13 GoodsBean goodsBean = allByUsername.get(position); 14 MyViewHolder holder = (MyViewHolder) holder2; 15 String goodsPath = goodsBean.getGoodsPath(); 16 System.out.println("goodsPath :" + goodsPath); 17 Bitmap bitmap = BitmapFactory.decodeFile(goodsPath); 18 holder.ivGoodsPic.setBackground(new BitmapDrawable(bitmap)); 19 holder.tvGoodsName.setText("商品名称:" + goodsBean.getGoodsName()); 20 holder.tvGoodsPrice.setText("商品价格:" + "¥" +goodsBean.getGoodsPrice()); 21 holder.tvGoodsCategory.setText("商品分类:" + goodsBean.getGoodsCategory()); 22 holder.tvGoodsPhone.setText("联系电话:" + goodsBean.getGoodsPhone()); 23 holder.tvGoodsTime.setText("发布时间:" + goodsBean.getGoodsTime() +" "); 24 holder.btnAddPurchase.setTag(position); 25 holder.btnFavourCancel.setTag(position); 26 if(flag==1 || flag==2){ 27 if(flag==1) { 28 holder.btnAddPurchase.setVisibility(View.GONE); 29 holder.btnFavourCancel.setVisibility(View.GONE); 30 } 31 if(flag==2){ 32 String id = goodsBean.get_id(); 33 holder.btnAddPurchase.setTag(id); 34 holder.btnFavourCancel.setVisibility(View.GONE); 35 holder.btnAddPurchase.setOnClickListener(listener1); 36 } 37 }else{ 38 String id = goodsBean.get_id(); 39 holder.btnFavourCancel.setTag(id); 40 holder.btnAddPurchase.setVisibility(View.GONE); 41 holder.btnFavourCancel.setOnClickListener(listener); 42 } 43 } 44 45 @Override 46 public int getItemCount() { 47 return allByUsername == null ? 0 : allByUsername.size(); 48 } 49 } 50 51 private View.OnClickListener listener1= new View.OnClickListener(){ 52 @Override 53 public void onClick(View v) { 54 String goodsId = (String) v.getTag(); 55 System.out.println("goodsId : " + goodsId); 56 int raw = ProductDao.getInstance().updateGoodsBuyed(goodsId); 57 if (raw > 0){ 58 UiUtils.toast("购买成功"); 59 }else { 60 UiUtils.toast("购买失败"); 61 } 62 } 63 }; 64 65 private View.OnClickListener listener = new View.OnClickListener(){ 66 @Override 67 public void onClick(View v) { 68 String goodsId = (String) v.getTag(); 69 System.out.println("goodsId : " + goodsId); 70 int raw = ProductDao.getInstance().updateGoodsBuyedDelete(goodsId); 71 if (raw == 0){ 72 UiUtils.toast("删除成功"); 73 allByUsername = ProductDao.getInstance().findAllGoodsBeanByUsername(); 74 releaseRecyclerView.setAdapter(new MyAdapter()); 75 }else { 76 UiUtils.toast("删除失败"); 77 } 78 } 79 }; 80 81 82 static class MyViewHolder extends RecyclerView.ViewHolder { 83 private ImageView ivGoodsPic; 84 private TextView tvGoodsName; 85 private TextView tvGoodsPrice; 86 private TextView tvGoodsCategory; 87 private TextView tvGoodsPhone; 88 private TextView tvGoodsTime; 89 private Button btnAddPurchase; 90 private Button btnFavourCancel; 91 92 93 public MyViewHolder(@NonNull View itemView) { 94 super(itemView); 95 ivGoodsPic = itemView.findViewById(R.id.ivGoodsPic); 96 tvGoodsName = itemView.findViewById(R.id.tvGoodsName); 97 tvGoodsPrice = itemView.findViewById(R.id.tvGoodsPrice); 98 tvGoodsCategory = itemView.findViewById(R.id.tvGoodsCategory); 99 tvGoodsPhone = itemView.findViewById(R.id.tvGoodsPhone); 100 tvGoodsTime = itemView.findViewById(R.id.tvGoodsTime); 101 btnFavourCancel = itemView.findViewById(R.id.tv_favour_cancel); 102 btnAddPurchase = itemView.findViewById(R.id.btnAddPurchase); 103 } 104 } 105 106 }