小编ars*_*gne的帖子

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 …

java sorting android arraylist

2
推荐指数
2
解决办法
9976
查看次数

标签 统计

android ×1

arraylist ×1

java ×1

sorting ×1