2017-02-28 63 views
1

我試圖在Firebase中的帖子中添加時間戳,但我很遺憾沒有成功。我已經嘗試了很多來自stackoverflow的建議,但都沒有成功。請幫助我如何在每篇文章下添加時間戳字段。將時間戳記發佈到Android Firebase數據庫不起作用

我想知道我的代碼有什麼問題。

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(); 

火力地堡數據庫結構:

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

向我們展示您的Firebase結構以及您想實現的目標? –

+0

{ 「博客」:{ 「-Ke1osQRFVs0fuqx9n18」:{ 「說明」: 「再一次」, 「UID」: 「FBwMzHJGP4U10LnLOwluy4BVyJ52」, 「用戶名」: 「OziBoo」 }} , 「用戶「:{ 」vi6Qd1AafidNGGV4roBhdLPZYGN2「:{ 」image「:」https://firebasestorage.googleapis.com/v0/b/agrodesk-b30ff.appspot.com/o/Profile_images%2Fcropped-377289343.jpg?alt=media&token = 5cf3ae8f-66af-43bd-bb8e-a9ee8e1250ff」, 「名」: 「OZI」 }} } 之前 –

+0

感謝您的幫助 –

回答

0

有代碼中的很多錯誤和誤用。首先請明白這一點:

  • 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數據庫的更多文檔,指南和教程。這可能需要很長時間,但一旦你瞭解它,其實很簡單。