Problem with documents and populating listview

Greetings to all,
Greetings, I am a populating a listview with documents from couchdb in a descending order, but whenever a document is updated it immediately moves to the top of the listview and when a new document is included it is added below the old document instead of on top of it. I understand that whenever changeEven is fired, code re-creates the data in Adapter and it re-populates the list view…How can I work around this? Such that when a new doc is added it goes to the top, and when a doc is changed it remains in the same position. Here is a code snippet of querying the view:

private void startLiveQuery() throws Exception {

        query = applicationz.getDatabase5().getView(String.format("%s/%s", designDocName, byDateViewName)).createQuery();

        liveQuery = query.toLiveQuery();
        liveQuery.addChangeListener(new LiveQuery.ChangeListener() {
            public void changed(final LiveQuery.ChangeEvent event) {

                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        for (Iterator<QueryRow> it = event.getRows(); it.hasNext(); ) {

Hey @mikekara,

Believe the document creation date, creationDate=... would be static here and can build your Views in the order of choice and run your Query accordingly like in this example.

This should reference the order that the document is created in regardless of any updates to an existing document in the datebase.

Hope that helps.

The list view is using the sort order defined by the keys in the CBL view being queried. If you want a different sort order, you’ll need to change the view and/or the document schema to emit the right keys.

In your case it sounds like you should store the document’s creation date (which would not be changed when you update the doc) and use that as the key. Then docs will be listed in order created. Of course you’ll want to reverse the order in the query.

hey @jens and @sweetiewill thanks for the reply. I have tried to emit the doc. based on the creationdate, but now it doesnt descend at all. Basically am trying to populate the listview if someones phone no. belongs to the field “p1phone” or “p2phone”. Here is the view:

 viewItemsByDate.setMap(new Mapper() {
            public void map(Map<String, Object> document, Emitter emitter) {
                Object createdAt = document.get("p1phone");
                Object created = document.get("p2phone");
                Object creat = document.get("oki");
                Object creationDate = document.get("created_at");
                String nulla = null;
                if (pho == null) {
                    emitter.emit(creat.toString(), null);
                } else {

                    if (createdAt.equals(nulla) ||created.equals(nulla)){

                       emitter.emit(creat.toString(), null);
                        //emitter.emit(creationDate.toString(), null);

                } else if(createdAt.equals(pho) && !createdAt.equals(nulla) ||created.equals(pho) ) {

                emitter.emit(createdAt.toString(), null);
                        //emitter.emit(creationDate.toString(), null);

                } else if(created.equals(pho) && !created.equals(nulla) ||createdAt.equals(pho)) {

                emitter.emit(created.toString(), null);
                       // emitter.emit(creationDate.toString(), null);
        }, "3.0");

Your map function is using a variable pho that isn’t declared inside the map function, so it must be something declared outside. That isn’t legal.

Remember that the map function’s job is to create an index. It doesn’t run at the time of the query, so it can’t use any state that’s specific to a query.