【问题标题】:Hide date in listview if it is from the same date如果日期来自同一日期,则在列表视图中隐藏日期
【发布时间】:2015-09-23 10:13:27
【问题描述】:

如果消息来自同一天,如何在我的 ListView 中隐藏日期?如果消息来自同一天,我想显示一次日期。

我的代码如下:

        final message Message = getItem(cursor.getPosition());

        //Get next chat message
        message nextChatMessage;

        if (cursor.getPosition() > 0) {
            //Move cursor forward
            nextChatMessage = getItem(cursor.getPosition() + 1);
        } else {
            nextChatMessage = null;
        }

        boolean isDiffDate = (nextChatMessage == null || !DateTimeUtil.isEqualDate(message.messageDate, nextChatMessage.messageDate));

获取物品:

    @Override
public Message getItem(int position) {
    Cursor cursor = getCursor();
    if(cursor != null && cursor.moveToPosition(getCount() - position - 1)) {
        message Message = new message ();
        Message .parseChatMessage(cursor);
        return Message ;
    } else {
        return null;
    }
}

谢谢。

【问题讨论】:

  • 您通常会在getView 中隐藏一个视图,所以这实际上是添加到您的问题中的有趣代码。
  • @SaschaKolberg,嗨。如果布尔值为 True,我会在 getView 中隐藏视图。
  • 嗯,日期是同一天,如果isDiffDatefalse不是 true。但是,知道DateTimeUtil.isEqualDate 返回什么也很有趣。
  • @SaschaKolberg,如果这两个日期不同,它将返回一个 true 并显示两个不同的日期。如果日期相同,则第一次只显示日期,隐藏其他日期相同的消息。但是,我的 'nextChatMessage' 没有指向下一个光标。因此,与当前日期比较的日期混淆了实际日期。请指教。谢谢。

标签: android date listview cursor adapter


【解决方案1】:

这可能是您希望在getView 中做的事情吗?

myDateView.setVisibility(isDiffDate ? View.VISIBLE : View.INVISIBLE);

【讨论】:

  • 嗨。谢谢你。我在 getView() 方法中设置了日期的可见性。就我而言,我希望如果两个日期不同,它会返回一个 true 并且它将显示两个不同的日期。如果日期相同,则仅首次显示日期,并隐藏其他日期相同的消息。但是,我的 'nextChatMessage' 没有指向下一个光标。因此,与当前日期比较的日期混淆了实际日期。请指教。谢谢。
【解决方案2】:

如果日期相同或不同,您必须设置视图的可见性 在适配器的getView()方法中

【讨论】:

  • 嗨。谢谢你。我在 getView() 方法中设置了日期的可见性。就我而言,我希望如果两个日期不同,它会返回一个 true 并且它将显示两个不同的日期。如果日期相同,则仅首次显示日期,并隐藏其他日期相同的消息。但是,我的 'nextChatMessage' 没有指向下一个光标。因此,与当前日期比较的日期混淆了实际日期。请指教。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多