【问题标题】:Listview displays wrong dataListview 显示错误的数据
【发布时间】: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


【解决方案1】:
  final Calendar c = Calendar.getInstance();       
  int  hour = c.get(Calendar.HOUR_OF_DAY);
  int minute = c.get(Calendar.MINUTE);    
  if (c.get(Calendar.AM_PM) == Calendar.AM){
     am_pm = "AM";
  }else if (c.get(Calendar.AM_PM) == Calendar.PM) 
     am_pm = "PM"; 

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
    String formattedDate = df.format(c.getTime());        
    Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();

//现在使用 formattedDate 并将其传递给您的 setText。 //去JodaTime,让生活更轻松

【讨论】:

  • 我认为问题不在于日期和时间,而是它显示项目的列表视图,即使用户没有发送如此奇怪的消息。
  • 这很好,但问题是即使用户没有发布消息,列表视图也会显示日期。
猜你喜欢
  • 1970-01-01
  • 2011-08-30
  • 2020-04-30
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多