I know this is old and i can find some popular post about this here. but i swear i am still stuck, i don't really understand every answer posted, it's been 7 hours, please be kind and provide me answer, this is killing me.
to make the answer is clear and direct, i decided to post one answer from other post that i think might be the answer, but i just don't know how to apply it to my code.
So, i use POJO :
HashMap<String, Object> timestamp = new HashMap<>();
public Reservation(String rid,Map timestamp, String status) {
this.rId = rid;
this.timestamp = timestamp;
this.status = status;
}
public Map getTimestamp() {
return timestamp;
}
and this is how i write to db :
private void SendReservation() {
final String key = mDatabaseRes.child("Reservations").push().getKey();
final Reservation reservation = new Reservation(MainActivity.currUser.getUID(),ServerValue.TIMESTAMP,"pending");
mDatabaseRes.child("Reservations").child(key).setValue(reservation, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference reference) {
if (databaseError != null) {
Log.e("Database ERROR", "Failed to write message", databaseError.toException());
} else {
Toast.makeText(getApplicationContext(),
"success"
, Toast.LENGTH_LONG).show();
}
}
});
}
until now i successfully write to db, the timestamp also display the time 14090282xxxx (seems correct until now), the error raised when reading the timestamp value, something like "cannot deserialize the long to map" at the snapshot get value :
reservationList.clear();
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
reservationList.add(dsp.getValue(Reservation.class)); //add result into array list
}
Okay, as i said, let me pick and show one answer here from kevin o'neill, how can i apply to mine : here is the link
Param param1;
Param param2;
HashMap<String, Object> timestampCreated;
//required empty constructor
public DataObject(){}
public DataObject(Param param1, Param param2) {
this.param1 = param1;
this.param2 = param2;
HashMap<String, Object> timestampNow = new HashMap<>();
timestampNow.put("timestamp", ServerValue.TIMESTAMP);
}
public HashMap<String, Object> getTimestampCreated(){
return timestampCreated;
}
@Exclude
public long getTimestampCreatedLong(){
return (long)timestampCreated.get("timestamp");
}
this is accepted answer with some votes , what confuse me is what is the connection between timestamnow and timestampcreated ? , also the constructor doesn't have timestamp like the one i have.
what i need to add to my code to make it get the right result.