Firebase Android如何从Model类本身推送数据

时间:2017-10-05 04:50:43

标签: android firebase firebase-realtime-database

我试图通过在该类中定义函数来将类的Object推送到Firebase,但是我得到 stackOverflow 错误。我在类中使用本机数据类型和Firebase引用对象。

我使用的数据库结构如下: Firebase Structure

我为订单创建了模型类,如下所示:

public class Orders {
private String billNo;
private String custId;
private String dateAndTime;
private int personCount;
private int status;
private int table;
private String orderId;
private boolean AC=true;

public String getBillNo() {
    return billNo;
}

public void setBillNo(String billNo) {
    this.billNo = billNo;
}

public String getCustId() {
    return custId;
}

public void setCustId(String custId) {
    this.custId = custId;
}

public String getDateAndTime() {
    return dateAndTime;
}

public void setDateAndTime(String dateAndTime) {
    this.dateAndTime = dateAndTime;
}

public int getPersonCount() {
    return personCount;
}

public void setPersonCount(int personCount) {
    this.personCount = personCount;
}

public int getStatus() {
    return status;
}

public void setStatus(int status) {
    this.status = status;
}

public int getTable() {
    return table;
}

public void setTable(int table) {
    this.table = table;
}

public String getOrderId() {
    return orderId;
}

public void setOrderId(String orderId) {
    this.orderId = orderId;
}


private DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();


public Orders(String orderId, String billNum, String customerId, String date, int personCnt, int status, int tableNo, boolean AC){
    this.billNo = billNum;
    this.custId = customerId;
    this.dateAndTime = date;
    this.personCount = personCnt;
    this.status = status;
    this.table = tableNo;
    this.orderId= orderId;

    Log.v("Orders","I am in Orders");
}

public Orders(){

}

public void pushOrder(){

    Log.v("PushOrdersId",this.getOrderId());
    String OId = this.getOrderId();

    mDatabase.child("Orders").child(OId).setValue(Orders.this);

}
}

然后我创建了子类Items ,如下所示:

public class Items extends Orders {
private String itemId;
private int qty;

DatabaseReference mRef = FirebaseDatabase.getInstance().getReference("Orders");

public Items(String orderId, String billNum, String customerId, String date, int personCnt, int status, int tableNo, boolean AC) {
    super(orderId, billNum, customerId, date, personCnt, status, tableNo, AC);
//        this.itemId = itemId;
//        this.qty = qty;
        Log.v("Items","I am in Items child");

}

public Items() {
}

public String getItemId() {
    return itemId;
}

public void setItemId(String itemId) {
    this.itemId = itemId;
}

public int getQty() {
    return qty;
}

public void setQty(int qty) {
    this.qty = qty;
}

//    public void push(HashMap<String, Items> items){
//        for (Items item: items.values()) {
//            String itemKey = mRef.child(this.getOrderId()).child("items").push().getKey();
//
//            mRef.child(this.getOrderId()).child("items").child(itemKey).setValue(item);
//        }
//    }
}

然后我在另一个类中调用Class Orders函数PushOrders:

nm=name.getText().toString();
                mob=mobile.getText().toString();
                seat=seats.getText().toString();
                email=emailID.getText().toString();


                    mDatabase = FirebaseDatabase.getInstance().getReference("Orders");
                    String orderTd = mDatabase.push().getKey();
                    custId = id;

                    itemsHashMap = new HashMap<>();

//                        itemsArrayMap.put("itemid","1");
//                        itemsArrayMap.put("quantity","2");
                    dateAndTime = new Date();
                    personCount=Integer.parseInt(seat);
                    //Toast.makeText(getApplicationContext(), orderTd, Toast.LENGTH_LONG).show();



                    Items item = new Items(orderTd,billNo, custId, dateAndTime.toString(), personCount, status, table, AC);

                    item.pushOrder();
//                        item.push(itemsHashMap);


                }

我能够在课程中找出以下行,订单正在给出错误,但无法找出原因?以及如何解决它。

mDatabase.child("Orders").child(OId).setValue(Orders.this);
  

错误日志

10-05 09:37:34.239 28265-28265/dyncardview.abpss.com E/AndroidRuntime: FATAL EXCEPTION: main Process: dyncardview.abpss.com, PID: 28265 java.lang.StackOverflowError: stack size 8MB at java.lang.reflect.Method.invoke(Native Method) at com.google.android.gms.internal.zzbqi$zza.zzaF

在这种情况下,有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

创建Firebase数据库实例后,您只需编写以下代码段:

User users = new User(userID,name,edtEmail.getText()。toString()。trim(),edtContactNumber.getText()。toString()。trim());     //将数据保存到数据库中     mDatabase.child(&#34;用户&#34)。子(用户ID).setValue(用户);

您可以根据要求更改模型和数据

答案 1 :(得分:0)

我已根据您的firebase数据库结构对您的代码进行了一些更改

//Set order data
Orders orders=new Orders();
orders.setBillNo("123");
orders.setCustId("12");
orders.setDateAndTime(new Date().toString());
orders.setPersonCount(13);
orders.setStatus(2);
orders.setTable(2);
orders.setOrderId("23");
orders.setAC(true);

//Add items to order
List<Items> itemsList=new ArrayList<Items>();
for(int i=0;i<2;i++){
    Items items=new Items();
    items.setItemId("55");
    items.setQty(i);
    itemsList.add(items);
}
orders.setItems(itemsList);

DatabaseReference mRef = FirebaseDatabase.getInstance().getReference("Orders");
mRef.push().setValue(orders);

您的订单

public class Orders {
    private String billNo;
    private String custId;
    private String dateAndTime;
    private int personCount;
    private int status;
    private int table;
    private String orderId;
    private boolean AC = true;
    private List<Items> items;
    //Generate getter and setters other code
}

项目类

public class Items {
    private String itemId;
    private int qty;
    //Generate getter and setters other code
}

您可以使用包含完整商品的订单键获取订单(您将在订单中获得完整的商品列表)

DatabaseReference mRef = FirebaseDatabase.getInstance().getReference("Orders").child(yourOrderKey);
mRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapshot:dataSnapshot.getChildren()) {
             Orders orders=postSnapshot.getValue(Orders.class);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});