1

所以我有一个最近的通话页面。我想按日期分隔我最近的通话(格式为 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);
    }

任何帮助都会很棒。谢谢

4

1 回答 1

1

我将字符缓冲区更改为字符串并且有效。这个 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);
        }
于 2014-01-07T18:36:48.410 回答