Android - 如何按日期对arrayList进行排序?

ars*_*gne 2 java sorting android arraylist

我正在尝试arrayList按日期排序.我每次收到通知时都会将日期存储在Firebase上,并从那里检索.我正在使用,Collections.sort但我不知道如何实现我的代码,因为我的日期格式以字符串格式的数字开头,如"2017年7月12日上午12:00".

我在stackoverflow上看到了一些这样的例子,但我不知道如何让它在我的情况下工作.我将在下面发布截图和我的代码.

日期未排序.

在此输入图像描述

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Rit*_*esh 9

解析日期后进行排序。

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);
                }
            }
        })
Run Code Online (Sandbox Code Playgroud)

如果您的日期采用其他格式,请相应地编写DateFormat

  • 不知道为什么这个答案有任何赞成票。它对我来说工作得很好。谢谢瑞泰什。 (2认同)

Spa*_*son 5

考虑将日期存储Notificationlong(unix时间)或a Date(LocalDateTime如果您正在使用Java 8支持),并仅在将其显示为UI时将其格式化为String.