2017-07-12 16 views
0

私は日付でarrayListをソートしようとしています。私は通知を受け取るたびにFirebaseに日付を保存し、そこから取得しました。私はCollections.sortを使用していますが、私のコードに実装する方法はわかりません。私の日付形式は「2017年12月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

ようこそスタックオーバーフロー!デバッグの助けを求める質問( "なぜこのコードは動作しないのですか?")には、目的の動作、_a特定の問題またはerror_を含める必要があります。明確な問題文がない質問は、他の読者にとって有用ではありません。参照:[最小限で完全で検証可能なサンプルの作成方法](http://stackoverflow.com/help/mcve) –

+0

getDate()はStringまたはDate 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); 
       } 
      } 
     }) 

あなたの日付は他のいくつかの形式である場合は、それに応じてにDateFormatを書きます。

5

は(あなたは、Java 8のサポートを使用している場合LocalDateTimelong(UNIX時間)としてNotificationの日付またはDateを格納し、UIにそれを表示するときにのみ、それを文字列として書式設定を検討してください。

+0

ああありがとう、私はそれを得ると思います。私はそれを私の「通知」の文字列として保存します – arsenallavigne

0

通知モデルで次のように日付を設定する代わりに。

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

System.currentTimeinillis();は、と私は理解したよう

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

を次のように表示する日付にそれを解析しながら、Notification.getDateは()String型の値を返します。したがって、使用する

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); 
} 

あなたの文字列から日付を削除するこの方法。 2つの日付を取得し、Date.compareToメソッドを使用します。

date1 = toDate(lhs.getDate()); 
date2 = toDate(rhs.getDate()); 
date1.compareTo(date2); 
関連する問題