2022-11-10学习内容
1.案例-购物车-加入购物车
1.1ShoppingChannelActivity.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.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 java.util.List; public class ShoppingChannelActivity extends AppCompatActivity { // 声明一个商品数据库的帮助器对象 private ShoppingDBHelper mDBHelper; private View 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(); } 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); // 给控件设置值 iv_thumb.setImageURI(Uri.parse(info.picPath)); tv_name.setText(info.name); tv_price.setText(String.valueOf((int)info.price)); // 把商品视图添加到网格布局 gl_channel.addView(view, params); } } @Override protected void onDestroy() { super.onDestroy(); mDBHelper.closeLink(); } }
1.2ShoppingDBHelper.java
package com.example.chapter06.database; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import com.example.chapter06.entity.GoodsInfo; import java.util.ArrayList; import java.util.List; public class ShoppingDBHelper extends SQLiteOpenHelper { private static final String DB_NAME = "shopping.db"; // 商品信息表 private static final String TABLE_GOODS_INFO = "GOODS_INFO"; // 购物车信息表 private static final String TABLE_CART_INFO = "CART_INFO"; private static final int DB_VERSION = 1; private static ShoppingDBHelper mHelper = null; private SQLiteDatabase mRDB = null; private SQLiteDatabase mWDB = null; private ShoppingDBHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } // 打开数据库的读连接 public SQLiteDatabase openReadLink() { if (mRDB == null || !mRDB.isOpen()) { mRDB = mHelper.getReadableDatabase(); } return mRDB; } // 打开数据库的写连接 public SQLiteDatabase openWriteLink() { if (mWDB == null || !mWDB.isOpen()) { mWDB = mHelper.getWritableDatabase(); } return mWDB; } // 关闭数据库连接 public void closeLink() { if (mRDB != null && mRDB.isOpen()) { mRDB.close(); mRDB = null; } if (mWDB != null && mWDB.isOpen()) { mWDB.close(); mWDB = null; } } // 利用单例模式获取数据库帮助器的唯一实例 public static ShoppingDBHelper getInstance(Context context) { if (mHelper == null) { mHelper = new ShoppingDBHelper(context); } return mHelper; } // 创建数据库,执行建表语句 @Override public void onCreate(SQLiteDatabase db) { // 创建商品信息表 String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_GOODS_INFO + "(_ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + " NAME VARCHAR NOT NULL," + " DESCRIPTION VARCHAR NOT NULL," + " PRICE FLOAT NOT NULL," + " PIC_PATH VARCHAR NOT NULL);"; db.execSQL(sql); // 创建购物车信息表 sql = "CREATE TABLE IF NOT EXISTS " + TABLE_CART_INFO + "(_ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + " GOODS_ID INTEGER NOT NULL," + " COUNT INTEGER NOT NULL);"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } // 添加多条商品信息 public void insertGoodsInfos(List<GoodsInfo> list) { // 插入多条记录,要么全部成功,要么全部失败 try { mWDB.beginTransaction(); for (GoodsInfo info : list) { ContentValues values = new ContentValues(); values.put("name", info.name); values.put("description", info.description); values.put("price", info.price); values.put("pic_path", info.picPath); mWDB.insert(TABLE_GOODS_INFO, null, values); } mWDB.setTransactionSuccessful(); } catch (Exception e) { e.printStackTrace(); } finally { mWDB.endTransaction(); } } // 查询所有的商品信息 public List<GoodsInfo> queryAllGoodsInfo() { String sql = "select * from " + TABLE_GOODS_INFO; List<GoodsInfo> list = new ArrayList<>(); Cursor cursor = mRDB.rawQuery(sql, null); while (cursor.moveToNext()) { GoodsInfo info = new GoodsInfo(); info.id = cursor.getInt(0); info.name = cursor.getString(1); info.description = cursor.getString(2); info.price = cursor.getFloat(3); info.picPath = cursor.getString(4); list.add(info); } return list; } }
1.3item_goods.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@color/white" android:gravity="center" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools"> <TextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/black" android:textSize="17sp" tools:text="小米手机"/> <ImageView android:id="@+id/iv_thumb" android:layout_width="180dp" android:layout_height="150dp" android:scaleType="fitCenter" tools:src="@drawable/xiaomi"/> <LinearLayout android:layout_width="match_parent" android:layout_height="45dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_price" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:gravity="center" android:textColor="@color/red" android:textSize="15sp" tools:text="20"/> <Button android:id="@+id/btn_add" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:gravity="center" android:text="加入购物车" android:textColor="@color/black" android:textSize="15sp"/> </LinearLayout> </LinearLayout>
1.4theme.xml
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
1.5效果