0

I'm not sure what to add to the following code to allow the post to have a time specified at the time the post was made...

    String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

public void SendButtonClicked(View view){
    FirebaseUser mCurrentUser = mAuth.getCurrentUser();
    DatabaseReference mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users").child(mCurrentUser.getUid());
    final String messageValue = editMessage.getText().toString().trim();
    if (!TextUtils.isEmpty(messageValue)){
        final DatabaseReference newPost = mDatabase.push();
        mDatabaseUsers.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                newPost.child("time").setValue(currentDateTimeString());
                newPost.child("content").setValue(messageValue);
                newPost.child("username").setValue(dataSnapshot.child("UserName").getValue()).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {

                    }
                });
            }

            private Object currentDateTimeString() {
                currentDateTimeString = 
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Do i need to implement a method, datasnapshot or would a string suffice?

Joshua Lee
  • 71
  • 7
  • I would suggest a long value rather than a string https://stackoverflow.com/a/833780/2308683 – OneCricketeer Apr 07 '18 at 05:10
  • Reasons - 1) Sorting numeric values is more performant, assuming you wanted to. 2) You should format the values in a UI. Databases don't have to store human readable data – OneCricketeer Apr 07 '18 at 05:12

0 Answers0