2022-11-11学习内容
1.案例-购物车-加入购物车
1.1MyApplication.java中新增内容:
// 购物车中的商品总数量 public int goodsCount;
1.2CartInfo.java
package com.example.chapter06.entity; // 购物车信息 public class CartInfo { public int id; // 商品编号 public int goodsId; // 商品数量 public int count; public CartInfo() {} public CartInfo(int id, int goodsId, int count) { this.id = id; this.goodsId = goodsId; this.count = count; } }
1.3ShoppingDBHelper.java中新增内容:
// 添加商品到购物车 public void insertCartInfo(int goodsId) { // 如果购物车中不存在该商品,添加一条信息 CartInfo cartInfo = queryCartInfoByGoodsId(goodsId); ContentValues values = new ContentValues(); values.put("goods_id", goodsId); if (cartInfo == null) { values.put("count", 1); mWDB.insert(TABLE_CART_INFO,null,values); } else { // 如果购物车中已经存在该商品,更新商品数量 values.put("_id", cartInfo.id); values.put("count", ++cartInfo.count); mWDB.update(TABLE_CART_INFO,values,"_id=?",new String[]{String.valueOf(cartInfo.id)}); } } private CartInfo queryCartInfoByGoodsId(int goodsId) { Cursor cursor = mRDB.query(TABLE_CART_INFO, null, "goods_id=?", new String[]{String.valueOf(goodsId)}, null, null, null); CartInfo info = null; if (cursor.moveToNext()) { info = new CartInfo(); info.id = cursor.getInt(0); info.goodsId = cursor.getInt(1); info.count = cursor.getInt(2); } return info; } // 统计购物车中商品的总数量 public int countCartInfo() { int count = 0; String sql = "select sum(count) from " + TABLE_CART_INFO; Cursor cursor = mRDB.rawQuery(sql, null); if (cursor.moveToNext()) { count = cursor.getInt(0); } return count; }
1.4ShoppingChannelActivity.java:
package com.example.chapter06; import androidx.appcompat.app.AppCompatActivity; import android.net.Uri; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.GridLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.example.chapter06.database.ShoppingDBHelper; import com.example.chapter06.entity.GoodsInfo; import com.example.chapter06.util.ToastUtil; import java.util.List; public class ShoppingChannelActivity extends AppCompatActivity { // 声明一个商品数据库的帮助器对象 private ShoppingDBHelper mDBHelper; private TextView tv_count; private GridLayout gl_channel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shopping_channel); TextView tv_title = findViewById(R.id.tv_title); tv_title.setText("手机商场"); tv_count = findViewById(R.id.tv_count); gl_channel = findViewById(R.id.gl_channel); mDBHelper = ShoppingDBHelper.getInstance(this); mDBHelper.openReadLink(); mDBHelper.openWriteLink(); // 从数据库查询出商品信息,并展示 showGoods(); } @Override protected void onResume() { super.onResume(); // 查询购物车商品总数,并展示 showCartInfoTotal(); } // 查询购物车商品总数,并展示 private void showCartInfoTotal() { int count = mDBHelper.countCartInfo(); MyApplication.getInstance().goodsCount = count; tv_count.setText(String.valueOf(count)); } private void showGoods() { // 商品条目是一个线性布局,设置布局的宽度为屏幕的一半 int screenWidth = getResources().getDisplayMetrics().widthPixels; LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth / 2, LinearLayout.LayoutParams.WRAP_CONTENT); // 查询商品数据库中的所有商品记录 List<GoodsInfo> list = mDBHelper.queryAllGoodsInfo(); for (GoodsInfo info : list) { // 获取布局文件item_goods.xml的根视图 View view = LayoutInflater.from(this).inflate(R.layout.item_goods, null); ImageView iv_thumb = view.findViewById(R.id.iv_thumb); TextView tv_name = view.findViewById(R.id.tv_name); TextView tv_price = view.findViewById(R.id.tv_price); Button btn_add = view.findViewById(R.id.btn_add); // 给控件设置值 iv_thumb.setImageURI(Uri.parse(info.picPath)); tv_name.setText(info.name); tv_price.setText(String.valueOf((int)info.price)); // 添加到购物车 btn_add.setOnClickListener(v -> { addToCart(info.id, info.name); }); // 把商品视图添加到网格布局 gl_channel.addView(view, params); } } // 把指定编号的商品添加到购物车 private void addToCart(int goodsId, String goodsName) { // 购物车商品数量+1 int count = ++MyApplication.getInstance().goodsCount; tv_count.setText(String.valueOf(count)); mDBHelper.insertCartInfo(goodsId); ToastUtil.show(this, "已添加一部" + goodsName + "到购物车"); } @Override protected void onDestroy() { super.onDestroy(); mDBHelper.closeLink(); } }
1.5效果:
点击“加入购物车”,购物车徽标数量加一:
多添加几个,然后结束掉进程,再次进入APP,可以看到购物车数量仍是正确的:
查看数据库,可以看到数据库也是正确的,1号商品1个,2号商品6个: