Firebase ServerValue.TIMESTAMP in Java data models objects

前端 未结 6 1352
[愿得一人]
[愿得一人] 2020-11-30 10:42

I\'m new to Firebase, and I\'ve been really enjoying it so far. I\'m running into a problem; I\'m using the FirebaseListAdapter similar to the tutorial outline here: https:

6条回答
  •  广开言路
    2020-11-30 11:03

    This sounds similar to this question: When making a POJO in Firebase, can you use ServerValue.TIMESTAMP?

    When creating POJOs used to store/retrieve data apart from the default empty constructor I usually use a constructor similar to this:

    Param param1;
    Param param2;
    HashMap timestampCreated;
    
    //required empty constructor
    public DataObject(){}
    
    public DataObject(Param param1, Param param2) {
           this.param1 = param1;
           this.param2 = param2;
           HashMap timestampNow = new HashMap<>();
           timestampNow.put("timestamp", ServerValue.TIMESTAMP);
           this.timestampCreated = timestampNow;
    }
    

    Be sure to include a getter for the HashMap<> used to store the Timestamp:

    public HashMap getTimestampCreated(){
        return timestampCreated;
    }
    

    Then use the @Exclude annotation to create a getter that you can use in your code to get the value of the timestamp if you need it. The @Exclude annotation will cause Firebase to ignore this getter and not look for a corresponding property

    @Exclude
    public long getTimestampCreatedLong(){
        return (long)timestampCreated.get("timestamp");
    }
    

提交回复
热议问题