私は日付でarrayList
をソートしようとしています。私は通知を受け取るたびにFirebaseに日付を保存し、そこから取得しました。私はCollections.sort
を使用していますが、私のコードに実装する方法はわかりません。私の日付形式は「2017年12月12日12時00分」のような文字列形式の数字から始まります。Android - arrayListを日付順に並べ替えるには?
私はこれのいくつかの例を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);
}
}
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;
}
}
ようこそスタックオーバーフロー!デバッグの助けを求める質問( "なぜこのコードは動作しないのですか?")には、目的の動作、_a特定の問題またはerror_を含める必要があります。明確な問題文がない質問は、他の読者にとって有用ではありません。参照:[最小限で完全で検証可能なサンプルの作成方法](http://stackoverflow.com/help/mcve) –
getDate()はStringまたはDate foramtを返しますか? –
@UsmanRana文字列形式です。通知を受けるたびにFirebaseに文字列として保存しました。 – arsenallavigne