我成功了某種方式如何從Firebase數據庫檢索時間戳,但現在它顯示錯誤的時間戳。它顯示的是這樣的:01-01-1970 05:30。令人驚訝的是,該值顯示在所有帖子中。時間戳甚至不改變。從firebase數據庫檢索錯誤的時間戳
博客觀點持有人:
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mview;
public BlogViewHolder(View itemView) {
super(itemView);
mview = itemView;
}
public void setTitle(String title) {
TextView post_title = (TextView) mview.findViewById(R.id.blog_title);
post_title.setText(title);
}
public void setDesp(String desp) {
TextView post_desp = (TextView) mview.findViewById(R.id.blog_desp);
post_desp.setText(desp);
}
public void setTimestampCreated(long timestamp) {
TextView show_ts = (TextView) mview.findViewById(blog_timestamp);
SimpleDateFormat sfd=new SimpleDateFormat("dd-MM-yyyy HH:mm");
Date date=new Date(timestamp);
show_ts.setText(sfd.format(date));
}
FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.blog_row,
BlogViewHolder.class, mDatabase
) {
@Override
protected void populateViewHolder(final BlogViewHolder viewHolder,final Blog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesp(model.getDesp());
viewHolder.setTimestampCreated(model.getTimestampCreated());
}
};
Blog.java
public class Blog {
String title;
String desp;
long timestamp;
public Blog() {
}
public Blog(String title, String desp, long timestamp) {
this.title = title;
this.desp = desp;
this.timestamp=timestamp;
}
public String getDesp() {
return desp;
}
public void setDesp(String desp) {
this.desp = desp;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public long getTimestampCreated() {
return timestamp;
}
public void setTimestampCreated(long timestamp) {
this.timestamp = timestamp;
}
數據庫參考:
public void startPosting() {
final DatabaseReference newPost = databaseReference.push();
String title_val = title.getText().toString().trim();
String desp_val = desp.getText().toString().trim();
newPost.child("title").setValue(title_val);
newPost.child("desp").setValue(desp_val);
newPost.child("timestamp").setValue(ServerValue.TIMESTAMP);
}
傳入'setTimestampCreated()'的'timestamp'爲零。將代碼發佈到您閱讀帖子的位置並調用'setTimestampCreated()'。 –
@BobSnyder我添加了上面的代碼。 –
請更新您的帖子,以顯示博客的整個班級,而不僅僅是獲取者/設置者。 –