【发布时间】:2020-04-08 10:07:37
【问题描述】:
多年来一直致力于此timestamp,并使用 StackOverflow 的大量帖子来帮助我完成它。终于将timestamp 与所有其他孩子一起保存在 Firebase 中以获取图片,但我无法让它出现在 TextView 中的应用程序中。
所以在我的PostActivity.java 类中,我使用Map 将timestamp 保存到Firebase,然后在我的PostAdapter.java 类中我有ViewHolders,因此它出现在我的RecyclerView 中HomeFragment,但它以 Long 值 41526276373 的形式出现,而不是应有的 SimpleDateFormat。
有人可以帮我吗?
下面有我的PostActivity.java 类和PostAdapter.java 类。
PostActivity.java
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
String postid = reference.push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("postid", postid);
hashMap.put("postimage", myUrl);
hashMap.put("description", txt_description.getText().toString());
hashMap.put("text_event", txt_event.getText().toString());
hashMap.put("text_location", txt_location.getText().toString());
hashMap.put("text_date_time", txt_date_time.getText().toString());
hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());
hashMap.put("timestamp", ServerValue.TIMESTAMP);
reference.child(postid).setValue(hashMap);
//Do I have to put here reference.updateChildren(hashmap)?
progressDialog.dismiss();
startActivity(new Intent(PostActivity.this, MainActivity.class));
finish();
} else {
Toast.makeText(PostActivity.this, "Unsuccessful. Try again", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(PostActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(this, "No Image Selected", Toast.LENGTH_SHORT).show();
}
}
public static String getTimeDate(long timestamp) {
try {
DateFormat dateFormat = DateFormat.getDateTimeInstance();
Date netDate = (new Date(timestamp));
return dateFormat.format(netDate);
} catch (Exception e){
return "timestamp";
}
}
PostAdapter.java
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.post_item, parent, false);
return new PostAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final Post post = mPost.get(position);
Glide.with(mContext).load(post.getPostimage())
.apply(new RequestOptions().placeholder(R.drawable.placeholderimg))
.into(holder.post_image);
if ("".equals(post.getTimestamp())) {
holder.timestamp.setVisibility(View.GONE);
} else {
if (post.getPostid() != null) {
holder.timestamp.setVisibility(View.VISIBLE);
holder.timestamp.setText(post.getTimestamp().toString());
}
}
PostAdapter.java 已更新
if ("".equals(post.getTimestamp())) {
holder.timestamp.setVisibility(View.GONE);
} else {
if (post.getPostid() != null) {
holder.timestamp.setVisibility(View.VISIBLE);
***holder.timestamp.setText(post.getTimeDate());***
}
}
***getTimeDate(post.getPostid(), holder.timestamp);***
public String getTimeDate(long timestamp) {
try {
DateFormat dateFormat = DateFormat.getDateTimeInstance();
Date netDate = (new Date(this.timestamp));
return dateFormat.format(netDate);
} catch (Exception e) {
throw e;
}
}
【问题讨论】:
-
顺便考虑扔掉长期过时且臭名昭著的麻烦
SimpleDateFormat和朋友,并将ThreeTenABP添加到您的Android项目中,以便使用java.time,现代Java日期和时间API .使用起来感觉好多了。
标签: java android firebase firebase-realtime-database timestamp