@Override
  public View getView(int position, View view, ViewGroup parent) {

    Category category = new Category();
    if (categories.size() == 0) {
      getCategories(getData());
    }
    category = getCategory(position);

    if (category != null) {
      view = inflater.inflate(R.layout.list_item_recent_threads_category, parent, false);
      TextView tv = (TextView) view.findViewById(R.id.chat_separator);
      tv.setText(category.getText() + " (" + category.getItemCount() + ")");
      return fillView(view, position);
    } else {
      view = inflater.inflate(R.layout.list_item_recent_threads, null);
      return fillView(view, position);
    }
  }
 private int getCategories(List<MessageThread> messageThreads) {
   final long week = (long) 7 * 24 * 60 * 60 * 1000;
   final long month = (long) 30 * 24 * 60 * 60 * 1000;
   int itemCount = 0;
   Category category = new Category();
   for (int i = 0; i < messageThreads.size(); i++) {
     if (new Date(messageThreads.get(i).getTime()).getYear() == new Date().getYear()
         && new Date(messageThreads.get(i).getTime()).getMonth() == new Date().getMonth()
         && new Date(messageThreads.get(i).getTime()).getDate() == new Date().getDate()) {
       if (category.getText() == null) {
         category.setIndex(i);
         category.setText("Today");
       }
       itemCount++;
     } else {
       if (itemCount != 0 && "Today".equals(category.getText())) {
         category.setItemCount(itemCount);
         categories.add(category);
         itemCount = 0;
         category = new Category();
       }
       Calendar cal = Calendar.getInstance();
       cal.add(Calendar.DATE, -1);
       if ((new Date(messageThreads.get(i).getTime()).getYear() == cal.getTime().getYear())
           && (new Date(messageThreads.get(i).getTime()).getMonth() == cal.getTime().getMonth())
           && (new Date(messageThreads.get(i).getTime()).getDate() == cal.getTime().getDate())) {
         if (category.getText() == null) {
           category.setIndex(i);
           category.setText("Yesterday");
         }
         itemCount++;
       } else {
         if (itemCount != 0 && "Yesterday".equals(category.getText())) {
           category.setItemCount(itemCount);
           categories.add(category);
           itemCount = 0;
           category = new Category();
         }
         if (new Date().getTime() - new Date(messageThreads.get(i).getTime()).getTime() < week) {
           if (category.getText() == null) {
             category.setIndex(i);
             category.setText("Earlier this week");
           }
           itemCount++;
         } else {
           if (itemCount != 0 && "Earlier this week".equals(category.getText())) {
             category.setItemCount(itemCount);
             categories.add(category);
             itemCount = 0;
             category = new Category();
           }
           if (new Date().getTime() - new Date(messageThreads.get(i).getTime()).getTime()
               < month) {
             if (category.getText() == null) {
               category.setIndex(i);
               category.setText("Earlier this month");
             }
             itemCount++;
           } else {
             if (itemCount != 0 && "Earlier this month".equals(category.getText())) {
               category.setItemCount(itemCount);
               categories.add(category);
               itemCount = 0;
               category = new Category();
             }
             if (new Date().getTime() - new Date(messageThreads.get(i).getTime()).getTime()
                 > month) {
               if (category.getText() == null) {
                 category.setIndex(i);
                 category.setText("More than month");
                 category.setItemCount(itemCount + messageThreads.size() - i);
                 categories.add(category);
                 return 1;
               }
               itemCount++;
             }
           }
         }
       }
     }
   }
   return 0;
 }