[英]timestamp method 'SimpleDateFormat' not retrieving from database
我遇到的問題是timestamp
沒有被保存,如下面的 Firebase 所示。 在Posts
沒有timestamp
子項。 有人可以向我解釋這是為什么嗎?
真的很感激有人看一眼並告訴我我做錯了什么。
下面是timestamp
的代碼。 PostAdapter.java
、 PostActivity.java
和Post.java
模型類。
發布活動.java
package com.e.events;
public class PostActivity extends AppCompatActivity {
Uri imageUri;
String myUrl = "";
StorageTask uploadTask;
StorageReference storageReference;
ImageView close, post_checkmark, image_added;
String description, text_event, text_location, text_date_time;
EditText txt_event, txt_description, txt_location, txt_date_time;
private String postId;
private String postImage;
private String publisher;
private Map<String, String> timestamp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
close = findViewById(R.id.close);
post_checkmark = findViewById(R.id.post_checkmark);
image_added = findViewById(R.id.new_image_added);
txt_description = findViewById(R.id.description);
txt_event = findViewById(R.id.text_event);
txt_location = findViewById(R.id.text_location);
txt_date_time = findViewById(R.id.text_date_time);
storageReference = FirebaseStorage.getInstance().getReference("posts");
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(PostActivity.this, MainActivity.class));
finish();
}
});
post_checkmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
}
});
CropImage.activity()
.setAspectRatio(1, 1)
.start(PostActivity.this);
}
private void saveTimestamp() {
Log.d(TAG, "saveTimestamp: getting timestamp");
FirebaseDatabase database = FirebaseDatabase.getInstance();
String key = database.getReference().child("Posts").push().getKey();
Post post = new Post(description, postId, postImage, publisher, timestamp, text_event, text_location, text_date_time);
database.getReference("Posts").child(key).setValue(post);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Map<String, Object> map = new HashMap<>();
map.put("timestamp", ServerValue.TIMESTAMP);
reference.child("Posts").setValue(map);
}
}
后適配器.java
package com.e.events.Adapter;
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
public Context mContext;
public List<Post> mPost;
private FirebaseUser firebaseUser;
public PostAdapter(Context mContext, List<Post> mPost) {
this.mContext = mContext;
this.mPost = mPost;
}
@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())) {
Log.i(TAG, post.getTimestamp().toString());
holder.timestamp.setVisibility(View.GONE);
} else {
holder.timestamp.setVisibility(View.VISIBLE);
holder.timestamp.setText(post.getTimestamp().toString());
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView timestamp;
public ViewHolder(@NonNull View itemView) {
super(itemView);
timestamp = itemView.findViewById(R.id.timestamp);
}
//Set words for the timestamp "Today" or "___ days ago"
private void setupWidgets() {
String timestampDifference = getTimestampDifference();
if (!timestampDifference.equals("0")) {
timestamp.setText(timestampDifference + " days ago");
} else {
timestamp.setText("Today");
}
}
//Timestamp
private String getTimestampDifference() {
Log.d(TAG, "getTimestampDifference: getting timestamp difference");
String difference = "";
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy'T'HH:mm:ss'Z'", Locale.US);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
Date today = calendar.getTime();
simpleDateFormat.format(today);
Date timestamp;
final String dateTime = simpleDateFormat.format(calendar.getTime());
try {
timestamp = simpleDateFormat.parse(dateTime);
difference = String.valueOf(Math.round(((today.getTime() - timestamp.getTime()) / 1000 / 60 / 60 / 24)));
} catch (ParseException e) {
difference = "0";
}
return difference;
}
}
private void getTimestamp() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Long timestamp = (Long) snapshot.getValue();
System.out.println(timestamp);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
后.java
package com.e.events.Model;
import android.widget.TextView;
import java.util.Map;
public class Post {
private String postid;
private String postimage;
private String description;
private String publisher;
private String text_event;
private String text_location;
private String text_date_time;
private Map<String, Object> timestamp;
public Post(String postid, String postimage, String description, String publisher, String text_event, String text_location,
String text_date_time, Map<String, Object> timestamp) {
this.postid = postid;
this.postimage = postimage;
this.description = description;
this.publisher = publisher;
this.text_event = text_event;
this.text_location = text_location;
this.text_date_time = text_date_time;
this.timestamp = timestamp;
}
public Post() {
}
public Post(String description, String postId, String postImage, String publisher, Map<String, String> textView, String text_event,
String text_location, String timestamp) {
}
public String getPostid() {
return postid;
}
public void setPostid(String postid) {
this.postid = postid;
}
public String getPostimage() {
return postimage;
}
public void setPostimage(String postimage) {
this.postimage = postimage;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getText_event() {
return text_event;
}
public void setText_event(String text_event) {
this.text_event = text_event;
}
public String getText_location() {
return text_location;
}
public void setText_location(String text_location) {
this.text_location = text_location;
}
public String getText_date_time() {
return text_date_time;
}
public void setText_date_time(String text_date_time) {
this.text_date_time = text_date_time;
}
public Map<String, Object> getTimestamp() {
return timestamp;
}
public void setTimestamp(Map<String, Object> timestamp) {
this.timestamp = timestamp;
}
}
如果要為每個帖子添加timeStamp
,則需要使用setValue()
:
FirebaseDatabase db = FirebaseDatabase.getInstance();
String key = db.getReference().child("Posts").push().getKey();
現在這將創建一個隨機 ID,並將該 ID 存儲在key
變量中。
然后你可以這樣做:
Posts post = new Post(description,postId,postImage,publisher,timestamp);
db.getReference("Posts").child(key).setValue(post);
如果您想通過覆蓋字段進行更新,則使用updateChildren()
。 但是您需要數據庫中已有的推送 ID timeStamp
在其下添加字段timeStamp
:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Map map = new HashMap();
map.put("timestamp", ServerValue.TIMESTAMP);
reference.child("Posts").child(key). updateChildren(map)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.