将购物车项目添加到数据库并检索
Adding Cart Items to the Database and retrieving
我在 mySQL 中有三个 table,即用户、餐厅、购物车列表。
此处用户table用于保存用户详细信息。
restaurant table 用于保存用餐详情、价格和数量。
cartlist 是添加餐厅的详细信息。
现在,当特定用户登录应用程序时,我希望将特定用户的购物车列表项目添加到列表中。但我正在获取将商品添加到购物车的所有用户的详细信息。
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String selectQuery = "SELECT * FROM restaurants";
SQLiteDatabase database = DatabaseManager.getInstance().openDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
ProductModel productModel = new ProductModel();
productModel.setID(Integer.parseInt(cursor.getString(0)));
productModel.setName(cursor.getString(1));
productModel.setMealname(cursor.getString(2));
productModel.setMealprice(cursor.getString(3));
productModel.setMealqty(cursor.getString(4));
products.add(productModel);
} while (cursor.moveToNext());
}
cursor.close();
DatabaseManager.getInstance().closeDatabase();
adapter = new ProductAdapter(ProductList.this, R.layout.activity_cartlistview, products);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
购物车table:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
lv_checkout = findViewById(R.id.lv_checkout);
tv_totalamount = findViewById(R.id.tv_totalamount);
btn_checkouttopayment = findViewById(R.id.btn_checkouttopayment);
String selectQuery = "SELECT * FROM carttable";
SQLiteDatabase database = DatabaseManager.getInstance().openDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
ProductModel productModel = new ProductModel();
productModel.setID(Integer.parseInt(cursor.getString(0)));
productModel.setName(cursor.getString(1));
productModel.setMealname(cursor.getString(2));
productModel.setMealprice(cursor.getString(3));
productModel.setMealqty(cursor.getString(4));
productModel.setMealtotal((Double.parseDouble(cursor.getString(3)) * Double.parseDouble(cursor.getString(4)) + ""));
products.add(productModel);
} while (cursor.moveToNext());
}
double totalPrices = 0;
for (int i = 0; i < products.size(); i++) {
totalPrices += Double.parseDouble(products.get(i).getMealtotal());
}
tv_totalamount.setText("Total Amount to be Paid : " + totalPrices + "");
cursor.close();
DatabaseManager.getInstance().closeDatabase();
adapter = new ProductAdapter(this, R.layout.activity_cartlistview, products);
lv_checkout.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
要对查询进行哪些更改?让我知道 guys.TIA!
这是因为您 运行 的查询将前往餐厅 table 并从中获取所有记录。据我所知,Cartlist 是 table 的名称,用户将在其中输入特定餐厅的详细信息。所以现在,您正在调用 Restaurant table 中的所有记录,无论是谁添加的,它都会为您提供列表。
您可能希望将查询更改为
SELECT restaurant_name FROM cartlist WHERE userName = selected_User;
现在你有餐厅名称了。保存这些并在查询时使用它。
SELECT * FROM restaurants WHERE restaurantsName = selected_restaurant_name;
do {
ProductModel productModel = new ProductModel();
productModel.setID(Integer.parseInt(cursor.getString(0)));
productModel.setName(cursor.getString(1));
productModel.setMealname(cursor.getString(2));
productModel.setMealprice(cursor.getString(3));
productModel.setMealqty(cursor.getString(4));
productModel.setMealtotal((Double.parseDouble(cursor.getString(3)) * Double.parseDouble(cursor.getString(4)) + ""));
products.add(productModel);
}
向您的模型添加构造函数 class 并注入。保存几行 code/readability。
do {
products.add( new ProductModel(
Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
}
行:productModel.setMealtotal((Double.parseDouble(cursor.getString(3)) * Double.parseDouble(cursor.getString(4)) + ""));
似乎包含一个简单的关系,其中 "Meal total = mean quantity * meal price" 也可以在构造函数中完成...它是一个常量算法并且是模型的一部分。
这是模型(我使用 lombok 添加了@Data,它在编译时生成了所有的 getter 和 setter)
import lombok.Data;
@Data
public class ProductModel {
private int id;
private String name;
private String mealName;
private int mealPrice;
private int mealQuantity;
private int mealTotal;
//Constructor...i.e. "new ProductModel(field1,field2...)"
public ProductModel(int id, String name, String mealName, int mealPrice, int mealQuantity) {
this.id = id;
this.name = name;
this.mealName = mealName;
this.mealPrice = mealPrice;
this.mealQuantity = mealQuantity;
this.mealTotal = mealPrice * mealQuantity;
}
}
我在 mySQL 中有三个 table,即用户、餐厅、购物车列表。
此处用户table用于保存用户详细信息。 restaurant table 用于保存用餐详情、价格和数量。 cartlist 是添加餐厅的详细信息。 现在,当特定用户登录应用程序时,我希望将特定用户的购物车列表项目添加到列表中。但我正在获取将商品添加到购物车的所有用户的详细信息。
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String selectQuery = "SELECT * FROM restaurants";
SQLiteDatabase database = DatabaseManager.getInstance().openDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
ProductModel productModel = new ProductModel();
productModel.setID(Integer.parseInt(cursor.getString(0)));
productModel.setName(cursor.getString(1));
productModel.setMealname(cursor.getString(2));
productModel.setMealprice(cursor.getString(3));
productModel.setMealqty(cursor.getString(4));
products.add(productModel);
} while (cursor.moveToNext());
}
cursor.close();
DatabaseManager.getInstance().closeDatabase();
adapter = new ProductAdapter(ProductList.this, R.layout.activity_cartlistview, products);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
购物车table:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
lv_checkout = findViewById(R.id.lv_checkout);
tv_totalamount = findViewById(R.id.tv_totalamount);
btn_checkouttopayment = findViewById(R.id.btn_checkouttopayment);
String selectQuery = "SELECT * FROM carttable";
SQLiteDatabase database = DatabaseManager.getInstance().openDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
ProductModel productModel = new ProductModel();
productModel.setID(Integer.parseInt(cursor.getString(0)));
productModel.setName(cursor.getString(1));
productModel.setMealname(cursor.getString(2));
productModel.setMealprice(cursor.getString(3));
productModel.setMealqty(cursor.getString(4));
productModel.setMealtotal((Double.parseDouble(cursor.getString(3)) * Double.parseDouble(cursor.getString(4)) + ""));
products.add(productModel);
} while (cursor.moveToNext());
}
double totalPrices = 0;
for (int i = 0; i < products.size(); i++) {
totalPrices += Double.parseDouble(products.get(i).getMealtotal());
}
tv_totalamount.setText("Total Amount to be Paid : " + totalPrices + "");
cursor.close();
DatabaseManager.getInstance().closeDatabase();
adapter = new ProductAdapter(this, R.layout.activity_cartlistview, products);
lv_checkout.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
要对查询进行哪些更改?让我知道 guys.TIA!
这是因为您 运行 的查询将前往餐厅 table 并从中获取所有记录。据我所知,Cartlist 是 table 的名称,用户将在其中输入特定餐厅的详细信息。所以现在,您正在调用 Restaurant table 中的所有记录,无论是谁添加的,它都会为您提供列表。 您可能希望将查询更改为
SELECT restaurant_name FROM cartlist WHERE userName = selected_User;
现在你有餐厅名称了。保存这些并在查询时使用它。
SELECT * FROM restaurants WHERE restaurantsName = selected_restaurant_name;
do {
ProductModel productModel = new ProductModel();
productModel.setID(Integer.parseInt(cursor.getString(0)));
productModel.setName(cursor.getString(1));
productModel.setMealname(cursor.getString(2));
productModel.setMealprice(cursor.getString(3));
productModel.setMealqty(cursor.getString(4));
productModel.setMealtotal((Double.parseDouble(cursor.getString(3)) * Double.parseDouble(cursor.getString(4)) + ""));
products.add(productModel);
}
向您的模型添加构造函数 class 并注入。保存几行 code/readability。
do {
products.add( new ProductModel(
Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
}
行:productModel.setMealtotal((Double.parseDouble(cursor.getString(3)) * Double.parseDouble(cursor.getString(4)) + ""));
似乎包含一个简单的关系,其中 "Meal total = mean quantity * meal price" 也可以在构造函数中完成...它是一个常量算法并且是模型的一部分。
这是模型(我使用 lombok 添加了@Data,它在编译时生成了所有的 getter 和 setter)
import lombok.Data;
@Data
public class ProductModel {
private int id;
private String name;
private String mealName;
private int mealPrice;
private int mealQuantity;
private int mealTotal;
//Constructor...i.e. "new ProductModel(field1,field2...)"
public ProductModel(int id, String name, String mealName, int mealPrice, int mealQuantity) {
this.id = id;
this.name = name;
this.mealName = mealName;
this.mealPrice = mealPrice;
this.mealQuantity = mealQuantity;
this.mealTotal = mealPrice * mealQuantity;
}
}