如何将firestore子集合文档转换为pojo类

时间:2019-05-03 13:35:31

标签: java android firebase google-cloud-firestore pojo

我的firestore模式是:

enter image description here

enter image description here

我试图获取存储在订单集合内的项子集合中的产品ID的值,但是当尝试获取orderModel.getItems()。getId()时,它返回null。

也orderModel.getItems()返回null。

我的情况是,我想在用户下订单时更新产品库存数量,所以我不想向Firestore提出要求,所以我是否可以从订单集合中获取产品ID。

这是我的Pojo课堂:

public class PlacedOrderModel {

public PlacedOrderModel() {
}

private String orderid,no_of_items,total_amount,delivery_date,payment_mode,deleiveryname,deliveryemail,deliverymobile_no,deliveryaddress,deliverypincode,status;
private String placed_user_name,Placed_user_email,Placed_user_mobile_no,getPlaced_user_id,expired_date;
private ProductModel items;

public String getExpired_date() {
    return expired_date;
}

public void setExpired_date(String expired_date) {
    this.expired_date = expired_date;
}

public ProductModel getItems() {
    return items;
}

public void setItems(ProductModel items) {
    this.items = items;
}

public PlacedOrderModel(String orderid, String no_of_items, String total_amount, String delivery_date, String payment_mode, String deleiveryname, String deliveryemail, String deliverymobile_no, String deliveryaddress, String deliverypincode, String placed_user_name, String getPlaced_user_email, String getPlaced_user_mobile_no, String getPlaced_user_id , String status) {

    this.orderid = orderid;
    this.no_of_items = no_of_items;
    this.total_amount = total_amount;
    this.delivery_date = delivery_date;
    this.payment_mode = payment_mode;
    this.deleiveryname = deleiveryname;
    this.deliveryemail = deliveryemail;
    this.deliverymobile_no = deliverymobile_no;
    this.deliveryaddress = deliveryaddress;
    this.deliverypincode = deliverypincode;
    this.placed_user_name = placed_user_name;
    this.Placed_user_email = getPlaced_user_email;
    this.Placed_user_mobile_no = getPlaced_user_mobile_no;
    this.getPlaced_user_id=getPlaced_user_id;
    this.status=status;
}

public String getStatus() {
    return status;
}

public String getOrderid() {
    return orderid;
}

public void setOrderid(String orderid) {
    this.orderid = orderid;
}

public String getPlaced_user_email() {
    return Placed_user_email;
}

public void setPlaced_user_email(String placed_user_email) {
    Placed_user_email = placed_user_email;
}

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

public String getGetPlaced_user_id() {
    return getPlaced_user_id;
}

public void setGetPlaced_user_id(String getPlaced_user_id) {
    this.getPlaced_user_id = getPlaced_user_id;
}

public String getPlaced_user_mobile_no() {
    return Placed_user_mobile_no;
}

public void setPlaced_user_mobile_no(String placed_user_mobile_no) {
    Placed_user_mobile_no = placed_user_mobile_no;
}

public String getNo_of_items() {
    return no_of_items;
}

public void setNo_of_items(String no_of_items) {
    this.no_of_items = no_of_items;
}

public String getTotal_amount() {
    return total_amount;
}

public void setTotal_amount(String total_amount) {
    this.total_amount = total_amount;
}

public String getDelivery_date() {
    return delivery_date;
}

public void setDelivery_date(String delivery_date) {
    this.delivery_date = delivery_date;
}

public String getPayment_mode() {
    return payment_mode;
}

public void setPayment_mode(String payment_mode) {
    this.payment_mode = payment_mode;
}

public String getDeleiveryname() {
    return deleiveryname;
}

public void setDeleiveryname(String deleiveryname) {
    this.deleiveryname = deleiveryname;
}

public String getDeliveryemail() {
    return deliveryemail;
}

public void setDeliveryemail(String deliveryemail) {
    this.deliveryemail = deliveryemail;
}

public String getDeliverymobile_no() {
    return deliverymobile_no;
}

public void setDeliverymobile_no(String deliverymobile_no) {
    this.deliverymobile_no = deliverymobile_no;
}

public String getDeliveryaddress() {
    return deliveryaddress;
}

public void setDeliveryaddress(String deliveryaddress) {
    this.deliveryaddress = deliveryaddress;
}

public String getDeliverypincode() {
    return deliverypincode;
}

public void setDeliverypincode(String deliverypincode) {
    this.deliverypincode = deliverypincode;
}

public String getPlaced_user_name() {
    return placed_user_name;
}

public void setPlaced_user_name(String placed_user_name) {
    this.placed_user_name = placed_user_name;
}

产品pojo类:

public class ProductModel implements Serializable, Parcelable {

  private String title , image , desc ,userEmail ,userMobile ,expireDate     ,id;
private int price  ,no_of_items ,currentStock , initStock;


private boolean isExpired;
public ProductModel(String id , String title, int price, String desc ,    String image, int no_of_items, String userEmail , String userMobile, String    expireDate, int currentStock , int initStock) {
    this.id=id;
    this.title = title;
    this.price=price;
    this.desc=desc;
    this.image = image;
    this.no_of_items=no_of_items;
    this.userEmail=userEmail;
    this.userMobile=userMobile;
    this.expireDate=expireDate;
    this.currentStock=currentStock;
    this.initStock=initStock;

}

public int getCurrentStock() {
    return currentStock;
}

public void setCurrentStock(int currentStock) {
    this.currentStock = currentStock;
}

public int getInitStock() {
    return initStock;
}

public void setInitStock(int initStock) {
    this.initStock = initStock;
}

public ProductModel() {



}

public boolean getExpireStatus(){
    String valid_until = getExpireDate();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date strDate = null;
    try {
        strDate = sdf.parse(valid_until);
    } catch (SafeParcelReader.ParseException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    if (new Date().after(strDate)) {

        isExpired = true;
    }
    else{
        isExpired = false;

    }
    return isExpired;
}
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getImage() {
    return image;
}

public int getNo_of_items() {
    return no_of_items;
}

public String getDesc() {
    return desc;
}

public int getPrice() {
    return price;
}

public String getExpireDate() {
    return this.expireDate;
}

@Override
public int describeContents() {
    return 0;
}


@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(title);
    dest.writeString(image);
    dest.writeString(desc);
    dest.writeString(userEmail);
    dest.writeString(userMobile);
    dest.writeString(id);
    dest.writeInt(no_of_items);
    dest.writeInt(price);
    dest.writeString(expireDate);
    dest.writeInt(currentStock);
    dest.writeInt(initStock);

}



public static final Parcelable.Creator<ProductModel> CREATOR
        = new Parcelable.Creator<ProductModel>() {
    public ProductModel createFromParcel(Parcel in) {
        return new ProductModel(in);
    }

    public ProductModel[] newArray(int size) {
        return new ProductModel[size];
    }
};

private ProductModel(Parcel in) {
    title = in.readString();
    image = in.readString();
    desc = in.readString();
    userEmail = in.readString();
    userMobile = in.readString();
    id = in.readString();
    no_of_items = in.readInt();
    price = in.readInt();
    expireDate = in.readString();
    currentStock = in.readInt();
    initStock = in.readInt();

}

0 个答案:

没有答案