2013-12-20 3 views
1

최근 전화 페이지가 있습니다. 최근 통화를 날짜 (MMMM DD, YYYY 형식)로 구분하고 싶지만 제대로 표시되지 않습니다. 마지막 세 항목이 2013 년 12 월 16 일, 2013 년 12 월 16 일, 2013 년 12 월 13 일인 경우에도 2013 년 12 월 17 일 첫 번째 날짜 만 표시됩니다.DateIndexer issue

여기 내 코드는 다음과 같습니다.

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); 
    } 

도움이 될 것입니다. 고마워요

답변

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); 
     } 
입니다