【问题标题】:error: method cleanCart in class Database cannot be applied to given types错误:类数据库中的方法 cleanCart 不能应用于给定类型
【发布时间】:2019-10-29 07:41:41
【问题描述】:

在食品应用程序上做项目时,我的 cleanCart() 方法遇到了问题。这是我的堆栈跟踪:

error: method cleanCart in class Database cannot be applied to given types;
                new Database(getBaseContext()).cleanCart();
                                              ^
  required: Order
  found: no arguments
  reason: actual and formal argument lists differ in length
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

这是我的 Cart.java 类

private void showAlertDialogue() {

        AlertDialog.Builder alertDialogue=new AlertDialog.Builder(Cart.this);
        alertDialogue.setTitle("One more step!");
        alertDialogue.setMessage("Enter your address :");



        final EditText edtAddress=new EditText(Cart.this);
        LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        );


        edtAddress.setLayoutParams(lp);
        alertDialogue.setView(edtAddress);  //Add edit text to alert dialog
        alertDialogue.setIcon(R.drawable.ic_shopping_cart_black_24dp);


        alertDialogue.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                //Create new request

                Request request=new Request(

                        Common.currentUser.getPhone(),
                        Common.currentUser.getName(),
                        txtTotalPrice.getText().toString(),
                        edtAddress.getText().toString(),
                        cart

                );


                //Submit to Firebase
                //We will using System.currentMili to key

                requests .child(String.valueOf(System.currentTimeMillis()))
                        .setValue(request);

//                        .setValue(request);

                //Delete cart

                new Database(getBaseContext()).cleanCart();

                Toast.makeText(Cart.this, "Thank you. Order placed", Toast.LENGTH_SHORT).show();
                finish();
            }
        });

为了帮助您找到错误,这是我的模型类 Order.java

public class Order {
    private String ProductId;
    private String ProductName;
    private String Quantity;
    private String Price;
    private String Discount;

    public Order() {
    }

    public Order(String productId, String productName, String quantity, String price, String discount) {
        ProductId = productId;
        ProductName = productName;
        Quantity = quantity;
        Price = price;
        Discount = discount;
    }

    public String getProductId() {
        return ProductId;
    }

    public void setProductId(String productId) {
        ProductId = productId;
    }

    public String getProductName() {
        return ProductName;
    }

    public void setProductName(String productName) {
        ProductName = productName;
    }

    public String getQuantity() {
        return Quantity;
    }

    public void setQuantity(String quantity) {
        Quantity = quantity;
    }

    public String getPrice() {
        return Price;
    }

    public void setPrice(String price) {
        Price = price;
    }

    public String getDiscount() {
        return Discount;
    }

    public void setDiscount(String discount) {
        Discount = discount;
    }
}

这是我的 Request.java 模型类

public class Request {

    private String phone;
    private String name;
    private String address;
    private String total;
    private List<Order> foods;  //List of food order

    public Request() {
    }

    public Request(String phone, String name, String address, String total, List<Order> foods) {
        this.phone = phone;
        this.name = name;
        this.address = address;
        this.total = total;
        this.foods = foods;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTotal() {
        return total;
    }

    public void setTotal(String total) {
        this.total = total;
    }

    public List<Order> getFoods() {
        return foods;
    }

    public void setFoods(List<Order> foods) {
        this.foods = foods;
    }
}

这是我的 Database.java 类

public class Database extends SQLiteAssetHelper {

    private static final String DB_NAME = "EatItDB.db";
    private static final int DB_VER = 1;

    public Database(Context context) {
        super(context, DB_NAME, null, DB_VER);
    }

    public List<Order> getCarts() {
        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        String[] sqlSelect = {"ProductId", "ProductName", "Quantity", "Price", "Discount"};
        String sqlTable = "OrderDetailId";

        qb.setTables(sqlTable);
        Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);

        final List<Order> result = new ArrayList<>();
        if (c.moveToFirst()) {
            do {
                result.add(new Order(c.getString(c.getColumnIndex("ProductId")),
                        c.getString(c.getColumnIndex("ProductName")),
                        c.getString(c.getColumnIndex("Quantity")),
                        c.getString(c.getColumnIndex("Price")),
                        c.getString(c.getColumnIndex("Discount"))

                ));
            } while (c.moveToNext());
        }
        return result;
    }

    public void addToCart(Order order) {
        SQLiteDatabase db = getReadableDatabase();
        String query = String.format("INSERT INTO OrderDetailID(ProductId,ProductName,Quantity,Price,Discount) VALUES('%s','%s','%s','%s','%s');",
                order.getProductId(),
                order.getProductName(),
                order.getQuantity(),
                order.getPrice(),
                order.getDiscount());

        db.execSQL(query);
    }

    public void cleanCart(Order order) {
        SQLiteDatabase db = getReadableDatabase();
        String query = String.format("DELETE FROM OrderDetail");

        db.execSQL(query);
    }
}

我还在我的 firebase 中发现了一个歧义,在地址字段中它显示了总金额

Requests
     1572338667924
      address: "$1,000.00"

foods
  name:"Tom cruise"
  phone:"0988123388"
  total:"USA"

但我仍然找不到修复它的方法。如有必要,我愿意进行更改。

【问题讨论】:

  • 看起来Database 类没有cleanCart 方法。 cleanCart方法写在哪里?
  • 兄弟 我现在已经添加了我的 Database.java 类。如果需要一些更改,请提出建议。
  • public void cleanCart(Order order) { - 带一个参数,不知道为什么,因为它没有被使用。
  • 请帮助大家..
  • 密切关注public void cleanCart(Order order) { 方法。它有一个参数Order order。但是参数没用。你可以删除它。

标签: java android database firebase methods


【解决方案1】:

这有点难以理解,但看起来这给了你一个错误,因为你的 cleanCart 方法有一个 Order order 参数,每个数据库类都需要传入该参数。

但是,当您调用该方法时,它会在没有任何参数的情况下被调用。

因为您根本没有在您的cleanCart 方法中使用Order 对象,您可以从方法中删除参数,如下所示:

数据库类:

public void cleanCart() { // remove parameter
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("DELETE FROM OrderDetail");

    db.execSQL(query);
}

【讨论】:

    猜你喜欢
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    相关资源
    最近更新 更多