【发布时间】:2017-03-14 07:03:52
【问题描述】:
我在 Android Studio + Firebase 中有一个简单的聊天应用程序,我正在显示用户电子邮件、消息以及日期和时间。问题是即使聊天室是空的,没有消息,列表视图项目的日期仍然显示,并且出于某种奇怪的原因显示 01-01-1970。
问题截图
chatMessage.setText(model.getChatMessage());
author.setText(model.getAuthor());
chatTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",model.getChatTime()));
public class chatAppGetter {
private String chatMessage;
private String author;
private String username;
private long chatTime;
public chatAppGetter(String chatMessage, String author) {
this.chatMessage = chatMessage;
this.author = author;
chatTime = new Date().getTime();
}
public chatAppGetter() {
}
public String getChatMessage() {
return chatMessage;
}
public void setChatMessage(String chatMessage) {
this.chatMessage = chatMessage;
}
public long getChatTime() {
return chatTime;
}
public void setChatTime(long chatTime) {
this.chatTime = chatTime;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
ListView messageList = (ListView)findViewById(R.id.listview);
mAdapter = new FirebaseListAdapter<chatAppGetter>(this, chatAppGetter.class, R.layout.chat_items, FirebaseDatabase.getInstance().getReference()) {
@Override
protected void populateView(View v, chatAppGetter model, int position) {
TextView chatMessage, author, chatTime;
chatMessage = (TextView)v.findViewById(R.id.chat_message);
author = (TextView)v.findViewById(R.id.chat_user);
chatTime = (TextView)v.findViewById(R.id.chat_time);
编辑:
问题解决了。我必须为消息创建一个单独的数据库分支。
【问题讨论】:
-
提供本地时区,或者你可以简单地使用jodatime日期格式
-
问题是即使用户没有发送消息,由于某种原因,日期仍然显示很奇怪。基本上,我使用 Dateformat.format("") 字符串来定义我的格式,因为它是一个字符串,即使没有用户发送消息,它也会显示在列表视图中。
-
然后发布您的代码
-
我添加了一些我认为问题所在的代码。我认为是因为这个 ("dd-MM-yyyy (HH:mm:ss)" 即使没有发布消息,它也会显示字符串。
-
如果用户没有发送消息,列表视图不应显示列表视图项。
标签: android listview firebase firebase-realtime-database