将购物车项目添加到数据库并检索

i7uq4tfw  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(237)

我在mysql中有三个表,即user、restaurants和cartlist。
用户表用于保存用户详细信息。餐厅餐桌是为了节省用餐的细节,价格和数量。cartlist将添加餐厅详细信息。现在当特定用户登录到应用程序时,我希望将特定用户的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();
        }
    });

推车:

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();
}

要对查询执行哪些更改?让我知道,伙计们。蒂亚!

p5cysglq

p5cysglq1#

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);

    }

将构造函数添加到模型类并插入。节省了几行代码/可读性。

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)) + "")); 似乎包含一个简单的关系,其中“膳食总量=平均数量*膳食价格”,这也可以在构造函数中完成…这是一个常数算法和模型的一部分。
下面是模型(我使用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;
    }
}
pcrecxhr

pcrecxhr2#

这是因为您正在运行的查询将转到餐厅表并从中获取所有记录。据我所知,cartlist是一个表的名称,用户可以在其中输入特定餐厅的详细信息。所以现在,您正在调用餐厅表中的所有记录,无论是谁添加的,这些记录都将为您提供列表。您可能需要将查询更改为

SELECT restaurant_name FROM cartlist WHERE userName = selected_User;

现在你有餐厅的名字了。保存这些并在查询时使用。

SELECT * FROM restaurants WHERE restaurantsName = selected_restaurant_name;

相关问题