android - 日期索引器问题

标签 android android-cursoradapter sectionindexer

所以我有一个最近的通话页面。我想按日期(格式为 MMMM DD, YYYY)分隔我最近的通话,但它没有正确显示。尽管最后三个条目是:2013 年 12 月 16 日、2013 年 12 月 16 日和 2013 年 12 月 13 日,但我只得到第一个日期 2013 年 12 月 17 日。

这是我的代码:

boolean needSeparator = false;

final int position = cursor.getPosition();

if(this.dateIndexer == null)
      this.dateIndexer = new LinkedHashMap<String, Integer>();

sdf = new SimpleDateFormat("h:mm aa MMMM dd, yyyy", Locale.getDefault());
Date testDate;
try {
          testDate = sdf.parse(date);
          Calendar calDate = Calendar.getInstance();
          calDate.setTime(testDate);
          String day = String.valueOf(calDate.get(Calendar.DATE));
          String month = calDate.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
          String year = String.valueOf(calDate.get(Calendar.YEAR));
          shortDate = month + " " + day + ", " + year;

          if(!shortDate.equals(prDate)){
              this.dateIndexer.put(shortDate, position);
              prDate = shortDate;
          }
} catch (ParseException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
}

    Set<String> sectionDates = this.dateIndexer.keySet();

    // create a list from the set to sort
    ArrayList<String> sectionList = new ArrayList<String>(sectionDates);

    this.sections = new String[sectionList.size()];
    sectionList.toArray(this.sections);

    if (position == 0) {
       needSeparator = true;
    } else {
       cursor.moveToPosition(position - 1);

       cursor.copyStringToBuffer(RecentQuery.COLUMN_DATE, mBuffer);
       if (mBuffer.sizeCopied > 0 && holder.dateBuffer.sizeCopied > 0 && mBuffer.data[0] != holder.dateBuffer.data[0]) {
           needSeparator = true;
       }

       cursor.moveToPosition(position);
    }

    if (needSeparator) {
       holder.separator.setText(sectionList.get(sectionList.size() - 1));
       holder.separator.setVisibility(View.VISIBLE);
    } else {
       holder.separator.setVisibility(View.GONE);
    }

任何帮助都会很棒。谢谢

最佳答案

我将字符缓冲区更改为字符串并且有效。这个 if 语句

if (mBuffer.sizeCopied > 0 && holder.dateBuffer.sizeCopied > 0 && mBuffer.data[0] != holder.dateBuffer.data[0]) {
       needSeparator = true;
}

只考虑了第一个字符(就像它应该的那样)。我将其用于我的联系页面,其中我只用字母分隔。因此更改它以比较字符串解决了这个问题。

这是新代码

boolean needSeparator = false;

final int position = cursor.getPosition();

if(this.dateIndexer == null)
   this.dateIndexer = new LinkedHashMap<String, Integer>();

sdf = new SimpleDateFormat("h:mm aa MMMM dd, yyyy", Locale.getDefault());
try {
    Calendar calDate = Calendar.getInstance();
    calDate.setTime(sdf.parse(date));
    String day = String.valueOf(calDate.get(Calendar.DATE));
    String month = calDate.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
    String year = String.valueOf(calDate.get(Calendar.YEAR));
    shortDate = month + " " + day + ", " + year;

    if(!shortDate.equals(prDate)){
       this.dateIndexer.put(shortDate, position);
       prDate = shortDate;
    }
} catch (ParseException e) {
   e.printStackTrace();
}
holder.dBuffer = shortDate;
Set<String> sectionDates = this.dateIndexer.keySet();

// create a list from the set to sort
ArrayList<String> sectionList = new ArrayList<String>(sectionDates);

this.sections = new String[sectionList.size()];
sectionList.toArray(this.sections);

        if (position == 0) {
            needSeparator = true;
        } else {
            cursor.moveToPosition(position - 1);

            sdf = new SimpleDateFormat("h:mm aa MMMM dd, yyyy", Locale.getDefault());
            try {
                Calendar calDate = Calendar.getInstance();
                calDate.setTime(sdf.parse(cursor.getString(RecentQuery.COLUMN_DATE)));
                String day = String.valueOf(calDate.get(Calendar.DATE));
                String month = calDate.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
                String year = String.valueOf(calDate.get(Calendar.YEAR));
                sBuffer = month + " " + day + ", " + year;

            } catch (ParseException e) {
                e.printStackTrace();
            }

            if (sBuffer.length() > 0 && holder.dBuffer.length() > 0 && !sBuffer.equals(holder.dBuffer)) {
                needSeparator = true;
            }

            cursor.moveToPosition(position);
        }

        if (needSeparator) {
            holder.separator.setText(String.valueOf(this.sections[this.sections.length - 1]));
            holder.separator.setVisibility(View.VISIBLE);
        } else {
            holder.separator.setVisibility(View.GONE);
        }

关于android - 日期索引器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20706449/

相关文章:

android - 光标未使用自定义适配器正确绑定(bind)文本

android - 从游标适配器删除和更新值

android - section indexer get section for position 返回 0

android - ListView快速滚动条bug

带有 SectionIndexer 的 Android RecyclerView

android - 无法在线程内创建处理程序

android - Adapter 中的 onScroll RecyclerView Timer 值变化

android - 为什么在屏幕底部对齐选项卡不是好的设计

android - SimpleCursorAdapter 的 onRestoreInstanceState

android - 如何在 IntentService 中使用 WebView?