0
votes

Ajout d'un nouvel article à 0 position dans Firestore?

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. 

0 commentaires

3 Réponses :


0
votes

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

Vous pouvez regarder ici https://firebase.google.com/docs / Firestore / Modèle de données


0 commentaires

0
votes

Pour résoudre ceci, veuillez modifier les lignes de code suivantes xxx

à xxx

conformément à la documentation officielle concernant l'arraylist Ajouter () Méthode:

insère l'élément spécifié à la position spécifiée dans cette liste.


0 commentaires

0
votes

Je résolvez ma requête en modifiant ce allNotifications.add (notifications);

à ce allnotifications.add (doc.getnewindex (), notifications);


0 commentaires