Android商城开发----点击加入购物车,购物车商品的增删减
上一章节:【分类页面】点击左侧类别,实现右侧对应类别商品的展示
本文是在上一章节基础上开发的,点击【分类】页面商品的加入购物车按钮,实现将商品加入购物车的功能。
目录
本文主要实现的功能是:
1.点击加入购物车按钮,将商品加入购物车。
2.点击相同的商品加入购物车,修改购物车中该商品数量 +1。
3.点击购物车页面 商品数量调节按钮【+1】按钮,商品数量 +1。
4.点击购物车页面中商品数量调节按钮【-1】按钮,商品数量 -1。
5.长摁删除购物车中该商品。
用到的主要知识点:
1.布局: RecyclerView的使用、对应Adapter的配置
2.数据库的创建: 我用的是SQLite,以及数据库操作:增、删、减、查
这里创建两个数据库:
一是 商品列表数据库(用来存放商品信息:商品Id、Icon、标题、价格)方便加入购物车时,按照Id去查找商品信息;
二是 购物车数据库(保存商品Id以及商品数量),方便对购物车商品进行操作增删减同时退出Activity后再回来商品数量仍然是上次退出时的显示,不会清零。
3.OKHttp网络请求
4.Json数据解析
5.封装类:(把对一类的操作封装进一个类里,方便复用,实现面向对象的操作)
两个类:商品类【 定义字段:商品Id 商品Icon 商品名称 商品价格;定义方法:增 查】和购物车类【定义字段:carId没用到 可以不定义,商品Id 商品数量;定义方法:增 改(加1.减1) 删 查】
6.接口: RecyclerView中Item点击、长摁、Item内部按钮的接口定义与调用,在接口里定义是为了区分出点的是哪个position的按钮。
点击将商品加入购物车的业务流程
源码
1.数据库
1)商品列表数据库GoodsListDatabase.java
package com.domain.mainView.database;
/*
* 商品列表数据库,将网络请求返回来的数据放进来
* */
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.nfc.Tag;
import android.util.Log;
import androidx.annotation.Nullable;
public class GoodsListDatabase extends SQLiteOpenHelper {
private static final String TAG=GoodsListDatabase.class.getSimpleName();
//定义创建数据表dict的SQL语句
//创建表时,定义的单个列的约束
public static final String CREATE_GOODS_SQL=
"create table goods(dbId integer ,dbIcon integer," +
"dbTitle text," +
"dbPrice text,UNIQUE(dbId))";
//private Context mContext;
//构造函数
public GoodsListDatabase(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_GOODS_SQL);
Log.i(TAG, "onCreate: 商品列表数据库创建成功");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists goods");
onCreate(db);
}
}
2)购物车数据库ShoppingCarDatabase.java
package com.domain.mainView.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
import java.io.DataOutputStream;
/*
* 购物车数据库,点击加入购物车图片按钮将该商品id加入购物车
* */
public class ShoppingCarDatabase extends SQLiteOpenHelper {
private static final String TAG=ShoppingCarDatabase.class.getSimpleName();
//定义创建数据表dict的SQL语句
final String CREATE_GOODS_SQL=
"create table shoppingCar(carId Integer primary key autoincrement,dbId Integer,dbNum Integer)";
//构造函数
public ShoppingCarDatabase(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_GOODS_SQL);
Log.i(TAG, "onCreate: 购物车数据库创建成功啦!");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//更新数据库
db.execSQL("drop table if exists shoppingCar");
onCreate(db);
}
}
2.封装类
1)Goods.java 类:商品类,用于封装对商品的操作
/*商品类
- 字段:商品Id 商品Icon 商品名称 商品价格
- 方法:增 查
- 通过网络请求获取商品信息 插入商品列表数据库
- 包含 对商品列表 加入商品方法和读取商品方法*/
package com.domain.mainView.mClass;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.domain.mainView.MyApplication;
import com.domain.mainView.database.GoodsListDatabase;
/*商品类
* 字段:商品Id 商品Icon 商品名称 商品价格
* 方法:增 查
* 通过网络请求获取商品信息 插入商品列表数据库
* 包含 对商品列表 加入商品方法和读取商品方法*/
public class Goods {
private int goodsId;
private int goodsIcon;
private String goodsTitle;
private String goodsPrice;
private static final String TAG = GoodsListDatabase.class.getSimpleName();
public Goods() {
}
public Goods(int goodsId, int goodsIcon, String goodsTitle, String goodsPrice) {
this.goodsId = goodsId;
this.goodsIcon = goodsIcon;
this.goodsTitle = goodsTitle;
this.goodsPrice = goodsPrice;
}
public int getGoodsId() {
return goodsId;
}
public int getGoodsIcon() {
return goodsIcon;
}
public String getGoodsTitle() {
return goodsTitle;
}
public String getGoodsPrice() {
return goodsPrice;
}
//插入商品 增
public void addGoods(int id, int icon, String title, String price) {
GoodsListDatabase goodsListDatabase = new GoodsListDatabase(MyApplication.getContext(), "goods.db", null, 2);
SQLiteDatabase db = goodsListDatabase.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("dbId", id);
values.put("dbIcon", icon);
values.put("dbTitle", title);
values.put("dbPrice", price);
//数据库构建中UNIQUE(dbId)与insert ignore into配合解决重复插入数据库的问题
String sql = "insert or ignore into goods(dbId,dbIcon,dbTitle,dbPrice)values('" + id + "','" + icon + "','" + title + "','" + price + "')";
db.execSQL(sql);
Log.i(TAG, "addGoods: 商品插入商品列表数据库成功拉!");
}
@SuppressLint("Range")
//查找商品 查
public Goods findGoods(int goodId) {
GoodsListDatabase goodsListDatabase = new GoodsListDatabase(MyApplication.getContext(), "goods.db", null, 2);
SQLiteDatabase db = goodsListDatabase.getWritableDatabase();
String idNew = String.valueOf(goodId);
int id = 0;
int icon = 0;
String title = "";
String price = "";
Cursor cursor = db.query("goods", null, "dbId= ?", new String[]{
idNew}, null, null, null);
if (cursor.moveToFirst()) {
do {
id = cursor.getInt(cursor.getColumnIndex("dbId"));
icon = cursor.getInt(cursor.getColumnIndex("dbIcon"));
title = cursor.getString(cursor.getColumnIndex("dbTitle"));
price = cursor.getString(cursor.getColumnIndex("dbPrice"));
} while (cursor.moveToNext());
}
Goods goods1 = new Goods(id, icon, title, price);
Log.i(TAG, "findGoods: goods1" + goods1);
Log.i(TAG, "findGoods: 读取购物