我能够生成一个时间戳并将其存储在Firebase Realtime Database中。但是无法弄清楚应该如何检索它并在textview中显示它。我知道它是作为一个长值检索并存储为Map 我还在模型类中创建了以下内容,但不知道下一步该怎么做。
private Map<String, String> timestamp;
发送消息的代码
private void sendMessage(String sender, final String receiver, String message) {
if (sender.equals(receiver)){
Toast.makeText(MessageActivity.this,"Message Can't be sent",Toast.LENGTH_SHORT).show();
}
else {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("sender", sender);
hashMap.put("receiver", receiver);
hashMap.put("message", message);
hashMap.put("timestamp", ServerValue.TIMESTAMP);
hashMap.put("isseen", false);
DatabaseReference Chatreference = FirebaseDatabase.getInstance().getReference("Chats");
Key = Chatreference.child(ChatKey).push().getKey();
Chatreference.child(ChatKey).child(Key).setValue(hashMap);
}
阅读邮件的代码
private void readMessages(String myid, String userid, String imageurl) {
mChat = new ArrayList<>();
Chatreference = FirebaseDatabase.getInstance().getReference("Chats").child(ChatKey);
Chatreference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Chat chat = dataSnapshot.getValue(Chat.class);
if (chat.getReceiver().equals(myid) && chat.getSender().equals(userid) ||
chat.getReceiver().equals(userid) && chat.getSender().equals(myid)) {
chat.setMessageId(dataSnapshot.getKey());
mChat.add(chat);
Log.d(TAG, "Child_Event_Listener_Called");
Log.d(TAG, "msg " + chat.getMessage());
}
messageAdapter = new MessageAdapter(MessageActivity.this, mChat, imageurl);
recyclerView.setAdapter(messageAdapter);
messageAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
Chat chat = dataSnapshot.getValue(Chat.class);
chat.setMessageId(dataSnapshot.getKey());
mChat.remove(chat);
messageAdapter.notifyDataSetChanged();
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
更新模型类
public class Chat {
private String sender;
private String receiver;
private String message;
private boolean isseen;
private Map<String, String> timestamp;
String MessageId;
public Chat(String sender, String receiver, String message, boolean isseen, Map<String, String> timestamp) {
this.sender = sender;
this.receiver = receiver;
this.message = message;
this.isseen = isseen;
this.timestamp = timestamp;
}
public Chat() {
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getReceiver() {
return receiver;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessageId() {
return MessageId;
}
public void setMessageId(String messageId) {
MessageId = messageId;
}
public boolean isIsseen() {
return isseen;
}
public void setIsseen(boolean isseen) {
this.isseen = isseen;
}
public Map<String, String> getTimestamp() {
return timestamp;
}
public void setTimestamp(Map<String, String> timestamp) {
this.timestamp = timestamp;
}
@Override
public boolean equals(@Nullable Object obj) {
if(obj instanceof Chat){
Chat chat=(Chat) obj;
return this.MessageId.equals(chat.getMessageId());}
else
return false;
}
}
答案 0 :(得分:1)
您要读取的是child
中的FirebaseDatabase
。以下是有关如何通过snapshot
读取子元素的示例,您将从firebase获得该元素。这里的模型有所不同,因为您的数据库中将具有不同的NoSql结构。
var userId = firebase.auth().currentUser.uid;
return firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
var username = (snapshot.val() && snapshot.val().username) || 'Anonymous';
// ...
});
此外,从Firebase返回的值可能是long
或String
,具体取决于您要存储时间戳的数据类型。看来它是以String
的形式发送的。
答案 1 :(得分:1)
首先,时间戳是一个长值而不是映射。您必须更改聊天类
//...
private boolean isseen;
private long timestamp;
String MessageId;
//...
然后您可以将时间戳转换为日期字符串,如下所示:
public static String longToDateString(long timestamp, String format){
return DateFormat.format(format,new Date(timestamp)).toString();
}
使用
String date = longToDateString(mChat.get(position).getTimestamp, "dd.MM.yyyyy");
或
String time = longToDateString(mChat.get(position).getTimestamp, "HH:mm");