android 时间大小排序,Android的 - 如何按日期排序arrayList?

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

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

日期沒有排序。

1eea6c641acc54504d141649f6da5635.png

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() {

@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中。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值