如何在Firebase中获取子节点的值并随后进行更新?

时间:2019-01-11 11:12:51

标签: java android firebase

我遵循了youtube上有关一个简单的Food Ordering应用程序的教程,但是该命令在没有product_stock的情况下仍然有效。我正在为“库存管理”添加产品库存功能。我的问题是,应用使用此计算总价,

cart = new Database(this).getCarts();
    adapter = new CartAdapter(cart, this);
    adapter.notifyDataSetChanged();
    recyclerView.setAdapter(adapter);

    //Calculation of total price
    int total = 0;
    for(Order order:cart)
        total+=(Integer.parseInt(order.getPrice())) * (Integer.parseInt(order.getQuantity()));

    Locale locale = new Locale("en","PH");
    NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);

    txtTotalPrice.setText(fmt.format(total));

还有这个

Request request = new Request(
                    Common.currentUser.getPhone(),
                    Common.currentUser.getName(),
                    edtAddress.getText().toString(),
                    txtTotalPrice.getText().toString(),
                    cart
            );
            // Submit to Firebase Database
            // We Will using System.CurrentMilli to Key
            requests.child(String.valueOf(System.currentTimeMillis()))
                    .setValue(request);

将请求插入Firebase数据库后,我要这样做

New_Stock = Current_Stock(FOOD.Stock.getValue() << I know this wont work - Quantity from >>>(Integer.parseInt(order.getQuantity()));

以便我更新产品库存,但是我不知道如何从“食物”表中获取Product_stock数据并从“订单”表中减去Order.Quantity。我很抱歉英语不好。

更新: 这是我的数据库结构。 water ordering database structure

更新2: 这是我的食物模型

public class Food {
private String Name, Image, Description, Price, MenuId, Stock;

public Food() {
}

public Food(String name, String image, String description, String price, String stock, String menuId) {
    Name = name;
    Image = image;
    Description = description;
    Price = price;
    Stock = stock;
    MenuId = menuId;
}
public String getName() {
    return Name;
}
public void setName(String name) {
    Name = name;
}
public String getImage() {
    return Image;
}
public void setImage(String image) {
    Image = image;
}
public String getDescription() {
    return Description;
}
public void setDescription(String description) {
    Description = description;
}
public String getPrice() {
    return Price;
}
public void setPrice(String price) {
    Price = price;
}
public String getStock() {
    return Stock;
}
public void setStock(String stock) {
    Stock = stock;
}
public String getMenuId() {
    return MenuId;
}
public void setMenuId(String menuId) {
    MenuId = menuId;
}
}

这是我的请求模型类

public class Request {
private String phone;
private String name;
private String address;
private String total;
private String status;
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;
    this.status = "0"; // Default is 0
}
public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}
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;
}
}

0 个答案:

没有答案