mFirestore.collection("notifications").orderBy("timestamp",Query.Direction.DESCENDING).addSnapshotListener(new EventListener<QuerySnapshot>(){ @Override public void onEvent(@javax.annotation.Nullable QuerySnapshot documentSnapshots,@javax.annotation.Nullable FirebaseFirestoreException e){ if(e!=null){ Log.d(TAG,"Error : "+e.getMessage()); } assert documentSnapshots!=null; for(DocumentChange doc:documentSnapshots.getDocumentChanges()){ if(doc.getType()==DocumentChange.Type.ADDED){ String doc_id=doc.getDocument().getId(); NotificationModel Notifications=doc.getDocument().toObject(NotificationModel.class).withDocId(doc_id); allNotifications.add(Notifications); notificationAdapter.notifyDataSetChanged(); } } } }); I am trying to do that when in firestore collection a new document is added it will add at 0 position. but instead of 0 position a new item is adding at the last position.
3 Réponses :
Les documents dans une collection Firestore ne sont pas stockés comme une matrice, mais plus sur une carte de la mode, ce qui signifie que vous ne pouvez pas insérer de document à une position spécifique p>
Vous pouvez regarder ici https://firebase.google.com/docs / Firestore / Modèle de données P>
Pour résoudre ceci, veuillez modifier les lignes de code suivantes à p> conformément à la documentation officielle concernant l'arraylist Ajouter () a > Méthode: P> insère l'élément spécifié à la position spécifiée dans cette liste. p>
blockQuote> p>
Je résolvez ma requête en modifiant ce à ce allNotifications.add (notifications); code> p>
allnotifications.add (doc.getnewindex (), notifications); code> p>