2017-07-12 3 views
0

Ich versuche meine arrayList nach Datum zu sortieren. Ich habe das Datum auf Firebase jedes Mal gespeichert, wenn ich eine Benachrichtigung erhalte und von dort abgerufen wird. Ich verwende Collections.sort, aber ich weiß nicht, wie ich meine Codes implementieren soll, da mein Datumsformat mit einer Nummer in einem Stringformat wie "12 Jul 2017 um 12:00 Uhr" beginnt.Android - Wie ArrayList nach Datum zu sortieren?

Ich habe einige Beispiele auf stackoverflow gesehen, aber ich weiß nicht, wie es in meinem Fall funktioniert. Ich werde Screenshots und meine Codes unten veröffentlichen.

Die Daten sind nicht sortiert.

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

Willkommen bei Stack Overflow! Fragen, die Debugging-Hilfe suchen ("Warum funktioniert dieser Code nicht?") Müssen das gewünschte Verhalten, ein bestimmtes Problem oder einen Fehler und den kürzesten Code enthalten, der zur Reproduktion in der Frage erforderlich ist. Fragen ohne eine klare Problemstellung sind für andere Leser nicht nützlich. Siehe: [Erstellen eines minimalen, vollständigen und überprüfbaren Beispiels] (http://stackoverflow.com/help/mcve). –

+0

Gibt getDate() String oder Date foramt zurück? –

+0

@UsmanRana Zeichenfolgeformat. Ich habe es jedes Mal als Zeichenfolge in Firebase gespeichert, wenn ich eine Benachrichtigung erhalte. – arsenallavigne

Antwort

0

Sortieren nach dem Parsing des Datums.

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

Wenn Sie das Datum in einem anderen Format ist, schreiben die Date entsprechend.

5

Betrachten Sie das Datum in Notification als long (Unix-Zeit) zu speichern oder ein Date (LocalDateTime wenn Sie Java 8-Unterstützung verwenden) und Formatierung es als nur String, wenn es um die Benutzeroberfläche angezeigt werden.

+0

Oh danke, ich denke ich verstehe es. Ich speichere es als String in meiner 'Benachrichtigung' – arsenallavigne

0

Anstatt das Datum wie folgt im Benachrichtigungsmodell festzulegen.

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

können Sie Zeit als System.currentTimeinMillis() speichern; und während der Anzeige dann analysieren Datum als

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

folgenden Wie ich verstehe, Notification.getDate() liefert String-Wert. Verwenden Sie also

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

diese Methode zu det Datum aus Ihrem String. Holen Sie sich einfach zwei Daten und verwenden Sie die Date.CompareTo-Methode.

date1 = toDate(lhs.getDate()); 
date2 = toDate(rhs.getDate()); 
date1.compareTo(date2);