So I have a recent call page. I want to separate my recent calls by the date (in the format MMMM DD, YYYY) but it doesn't show up properly. I only get the first date , December 17th, 2013, to show up even though the last three entries are: December 16th, 2013, December 16th, 2013, and December 13th, 2013.
Here is my code for this:
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);
}
Any help will be great. Thanks
I changed the char buffers to strings and that worked. This if statement
if (mBuffer.sizeCopied > 0 && holder.dateBuffer.sizeCopied > 0 && mBuffer.data[0] != holder.dateBuffer.data[0]) {
needSeparator = true;
}
Was only taking the first character into account(Like it should). I used this for my contact page where I was separating by only letters. So changing it to compare strings fixed the issue.
Here is the new code
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);
}