Léonce Stekendak
Léonce Stekendak

Reputation: 23

Posting timestamp to Android Firebase database doesn't work

I'm trying since a while to add timestamp on my posts in Firebase, but I'm sadly unsuccessful. I have already tried many advises from stackoverflow, but none worked. Please help me on how to add a timestamp field under each post.

I would to know what's wrong with my code.

final DatabaseReference newPost = mDatabase.push();
mDatabaseUser.addValueEventListener(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot dataSnapshot) {

       Long timestamp = (Long) dataSnapshot.getValue();
       System.out.println(timestamp);

       newPost.child("title").setValue(title_val);
       newPost.child("desc").setValue(desc_val);
       newPost.child("image").setValue(downloadUrl.toString());
       newPost.child("uid").setValue(mCurrentUser.getUid());
       newPost.child("username").setValue(dataSnapshot.child("name").getValue()).addOnCompleteListener(new OnCompleteListener<Void>() {
           @Override
           public void onComplete(@NonNull Task<Void> task) {

               if (task.isSuccessful()) {

                   startActivity(new Intent(PostActivity.this, MainActivity.class));
               }
           }
       });

   }

   @Override
   public void onCancelled(DatabaseError databaseError) {

   }
});

mDatabaseUser.setValue(ServerValue.TIMESTAMP);
mProgress.dismiss();

Firebase Database structure:

{  
   "Blog":{  
      "-Ke1osQRFVs0fuqx9n18":{  
         "desc":"again again",
         "uid":"FBwMzHJGP4U10LnLOwluy4BVyJ52",
         "username":"OziBoo"
      }
   },
   "Users":{  
      "vi6Qd1AafidNGGV4roBhdLPZYGN2":{  
         "image":"firebasestorage.googleapis.com/v0/b/agrodesk-b30ff.appspot.‌​com/...",
         "name":"Ozi"
      }
   }
}

Upvotes: 2

Views: 444

Answers (1)

koceeng
koceeng

Reputation: 2163

There are a lot of error and misuse in your code. Please understand this first:

  • ref.addValueEventListener(...) is used for listening to every changes made in data referenced by ref
  • ref.setValue(yourValue) is used to set the value of data referenced by ref object
  • setValue(...).addOnCompleteListener(...) is used if you want to execute something after value has been updated

If I understand it correctly, all of your sample code you write for writing value into database, right? But you, not knowingly, used addValueEventListener() instead.

So your code to write the value into new child inside "Blog" should be like this:

// Here I use HashMap to make it more simple
// You can (and better to) use your custom object as value container
HashMap<String, Object> value = new HashMap<>();
value.put("title", "your-title");
value.put("desc", "your-desc");
value.put("timestamp", ServerValue.TIMESTAMP);
// ... etc

// the following code will create a reference object pointing at "Blog"
DatabaseReference ref = FirebaseDatabase.getInstance().getRreference("Blog");

// the following code will make a new child inside data referenced by ref (in this case, "Blog")
DatabaseReference newBlog = ref.push();

// the following code is the code that actually update the data of referenced point
newBlog.setValue(value)
    // addOnCompleteListener is optional
    .addOnCompleteListener(new ... {
        // code placed here will be executed when your data is updated
        ...
    });

Hope this helps.

Note:

There I just show you what you want to achieve for this case and this case only. Please read more documentation, guide, and tutorial about Firebase Database. It might take long, but once you understand it, it's actually quite simple.

Upvotes: 2

Related Questions