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