2017-07-12 146 views
0

我想按日期排序我的arrayList。我每次收到通知時都會在Firebase上存儲日期,並從該處檢索。我使用Collections.sort,但我不知道如何實現我的代碼,因爲我的日期格式以字符串格式的數字開頭,如「2017年7月12日上午12:00」。Android的 - 如何按日期排序arrayList?

我已經看到了這個在stackoverflow的一些例子,但我不知道如何使它在我的情況下工作。我會在下面發佈截圖和我的代碼。

日期沒有排序。

enter image description here

NotificationFragment.java

public class NotificationFragment extends Fragment { 
     prepareNotification1(); 
     sortDate(); 
     return v; 
    } 

     private void prepareNotification1() { 
      FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 
      String userID = user.getUid(); 

    mRef.child("customers").child(userID).child("Notification").addChildEventListener(new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
        Notification menu = dataSnapshot.getValue(Notification.class); 
        notificationList.add(menu); 
       mAdapter.notifyDataSetChanged(); 
      } 
}); 
    } 

    public void sortDate() { 
     Collections.sort(notificationList, new Comparator<Notification>() { 
      @Override 
      public int compare(Notification lhs, Notification rhs) { 
       return lhs.getDate().compareTo(rhs.getDate()); 
      } 
     }); 
     mAdapter = new NotificationAdapter(getContext(), notificationList); 
     mRecyclerView.setAdapter(mAdapter); 
    } 
} 

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService { 
@Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Calendar cal = Calendar.getInstance(); 

     String currentDateTimeString = DateFormat.getDateInstance().format(new Date()); 

     SimpleDateFormat df = new SimpleDateFormat("hh:mm a"); 
     String currentTime = df.format(cal.getTime()); 

     String notificationTime = currentDateTimeString + " at " + currentTime; 

     Notification newNotification = new Notification(remoteMessage.getData().get("body"), notificationTime); 
     mRef.child("customers").child(userID).child("Notification").push().setValue(newNotification); 
} 

Notification.java

public class Notification { 
    private String message; 
    private String date; 


    public Notification(){ 
    } 

    public Notification(String message, String date){ 
     this.message = message; 
     this.date = date; 
    } 

    public String getDate() { 
     return date; 
    } 

    public void setDate(String date) { 
     this.date = date; 
    } 

    public String getMessage() { 
     return message; 
    } 

    public void setMessage(String message) { 
     this.message = message; 
    } 
} 
+0

歡迎來到Stack Overflow!尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須在問題本身中包含所需的行爲,特定的問題或錯誤以及必要的最短代碼**。沒有明確問題陳述的問題對其他讀者無益。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

+0

是getDate()返回字符串或日期foramt? –

+0

@UsmanRana字符串格式。每次收到通知時,我都會將其作爲字符串存儲在Firebase中。 – arsenallavigne

回答

0

解析日期後進行排序。

Collections.sort(notificationList, new Comparator<Notification>() { 
      DateFormat f = new SimpleDateFormat("dd/MM/yyyy '@'hh:mm a"); 
      @Override 
      public int compare(Notification lhs, Notification rhs) { 
       try { 
        return f.parse(lhs.getDate()).compareTo(f.parse(rhs.getDate())); 
       } catch (ParseException e) { 
        throw new IllegalArgumentException(e); 
       } 
      } 
     }) 

如果你的日期是在一些其他的格式,寫上日期格式相應

5

考慮存儲Notification日期爲long(UNIX時間)或DateLocalDateTime,如果你使用的是Java 8支持),並將其顯示到用戶界面,只有當格式化爲一個字符串。

+0

哦,謝謝,我想我明白了。我將它作爲字符串存儲在「通知」中 – arsenallavigne

0

而不是在通知模型中設置如下所示的日期。

`String notificationTime = currentDateTimeString + " at " + currentTime;` 

可以節省時間System.currentTimeinMillis();和同時顯示那麼它解析到日期如下

DateFormat f = new SimpleDateFormat("dd/MM/yyyy hh:mm a"); 
f.parse(timeStamp); 
0

據我所知,Notification.getDate()返回字符串值。所以,使用

public static Date toDate(String value) throws ParseException { 
    DateFormat format = new SimpleDateFormat("d MMMM yyyy 'at' HH:mm'am'", Locale.ENGLISH); 
    return format.parse(value); 
} 

這種方法來從你的字符串det日期。只需獲取兩個日期並使用Date.compareTo方法即可。

date1 = toDate(lhs.getDate()); 
date2 = toDate(rhs.getDate()); 
date1.compareTo(date2);