【问题标题】:Posting timestamp to Android Firebase database doesn't work将时间戳发布到 Android Firebase 数据库不起作用
【发布时间】:2017-07-18 21:53:15
【问题描述】:

一段时间以来,我一直在尝试在 Firebase 中的帖子上添加时间戳,但遗憾的是我没有成功。我已经尝试了许多来自 stackoverflow 的建议,但没有一个奏效。请帮助我了解如何在每个帖子下添加时间戳字段。

我想知道我的代码出了什么问题。

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 数据库结构:

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

【问题讨论】:

  • 向我们展示您的 Firebase 结构以及您想要实现的目标?
  • {“博客”:{“-Ke1osQRFVs0fuqx9n18”:{“desc”:“再次”,“uid”:“FBwMzHJGP4U10LnLOwluy4BVyJ52”,“用户名”:“OziBoo”}},“用户” : { "vi6Qd1AafidNGGV4roBhdLPZYGN2" : { "image" : "firebasestorage.googleapis.com/v0/b/agrodesk-b30ff.appspot.com/…", "name" : "Ozi" } } }
  • 感谢您的帮助
  • 我已经编辑了您的问题并将结构添加到其中。下次,如果您想添加有关您的问题的更多详细信息,您可以编辑它而不是在评论中提及它:)

标签: android firebase firebase-realtime-database timestamp


【解决方案1】:

您的代码中有很多错误和误用。请先理解这一点:

  • ref.addValueEventListener(...) 用于监听ref 引用的数据中所做的每一次更改
  • ref.setValue(yourValue)用于设置ref对象引用的数据的值
  • setValue(...).addOnCompleteListener(...) 用于在值更新后执行某些操作

如果我理解正确,您为将值写入数据库而编写的所有示例代码,对吧?但是你却在不知不觉中使用了addValueEventListener()

所以您将值写入"Blog" 内的新子项的代码应该是这样的:

// 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
        ...
    });

希望这会有所帮助。

注意:

我只是向您展示了您希望在此案例中实现的目标,并且仅针对此案例。请阅读有关 Firebase 数据库的更多文档、指南和教程。这可能需要很长时间,但一旦你理解它,它实际上很简单。

【讨论】:

    猜你喜欢
    • 2017-02-07
    • 2018-01-25
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2019-12-20
    • 2019-07-22
    相关资源
    最近更新 更多