Android query to listview

looking for solution for Android couchbase lite and Listview that is grouped by truck.

my data looks like this

{
"_sync": {
“rev”: “1-15ec1088b51e6908bd29659acdf10cf6”,
“sequence”: 1017,
“recent_sequences”: [
1017
],
“history”: {
“revs”: [
“1-15ec1088b51e6908bd29659acdf10cf6”
],
“parents”: [
-1
],
“bodies”: [
""
],
“channels”: [
null
]
},
“time_saved”: “2017-08-25T14:50:17.3120584-06:00”
},
“checkIn”: [
“Air Lines”
],
“date”: “Fri Aug 25 14:54:39 MDT 2017”,
“emp”: “huhj09”,
“milage”: “2”,
“notes”: [
""
],
“photos”: [],
“trip”: “PreTrip”,
“truck”: “Truck_129”,
“type”: “dvir”
}

I have tried a few types of mapping and have not been able to get data returned.

I have used the tutorial code and get error

com.couchbase.lite.View listsView = mDatabase.getView(“list/listsByName”);
if (listsView.getMap() == null) {
listsView.setMap(new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
String type = (String) document.get(“type”);
if (“dvir”.equals(type)) {
emitter.emit(document.get(“name”), null);
}
}
}, “1.0”);
}

Well, your document doesn’t have a name property, so the emit call is going to fail. Sounds like you want to emit the truck property instead.

Thank you, yes I do want to emit the “truck” property. My code now is :

private void populateListView() {
com.couchbase.lite.View listsView = database.getView(“list/listsByName”);
if (listsView.getMap() == null) {
listsView.setMap(new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
String type = (String) document.get(“type”);
if (“dvir”.equals(type)) {
emitter.emit(document.get(“truck”), null);
}
}
}, “1.0”);
}
Log.d("log-Mspp-Query: ", “how to log result?”);
}

how do i get result? much thanks

You create a Query from the View, set options if you want, and run it. The documentation should show examples.

trucks = listView.creatQuery();

has an error on the trucks variable.

the documentation shows:
listsLiveQuery = listsView.createQuery().toLiveQuery();

I am looking for not live and getting array or objects.

I have changed to:
Query trucks = listsView.createQuery();

and no error but how do i drill into results?

https://developer.couchbase.com/documentation/mobile/1.4/guides/couchbase-lite/native-api/query/index.html#story-h2-3

Thank you i have it working now and with grouping and displaying in listView, what do you think?

public void setupViewAndQuery(){
        com.couchbase.lite.View listsView = database.getView("dvirs/all");
        if (listsView.getMap() == null) {
            listsView.setMap(new Mapper() {
                @Override
                public void map(Map<String, Object> document, Emitter emitter) {
                    String type = (String) document.get("type");
                       if ("dvir".equals(type)) {
                        emitter.emit(document.get("truck"), null);
                    }
                }
            }, "1.0");
        }
        Query trucksQuery = listsView.createQuery();
        QueryEnumerator result = null;
        try {
            result = trucksQuery.run();
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
        List<String> truckNames = new ArrayList<String>();
        for (Iterator<QueryRow> it = result; it.hasNext(); ) {
            QueryRow row = it.next();
            if(truckNames.contains(row.getKey())){
           // do nothing
            }else {
                truckNames.add((String)row.getKey());
            }
        };
        //Build the adapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.items , truckNames);
        //Configure the list view
        ListView list = (ListView) findViewById(R.id.listViewMain);
        list.setAdapter(adapter);
    }

I took the liberty of editing your post to format your source code — just added a line of three back-quotes before and after the code. Makes it easier to read.

The code looks sound (though Java’s not my best language). As an optimization & simplification, you can call trucksQuery.setGroupLevel(1): this will group together all rows with the same key, so you only get the unique keys in the result.

(Also, if you ever need to do this kind of merging manually: it’s much more efficient to use a Set, preferably a HashSet, instead of a List to collect the results, because contains is much faster. The code you have above runs in O(n^2) time, while using a HashSet it would be roughly O(n). Then afterwards you can copy the Set to a List and sort it.)

Great thank you.
Where do i find info about HashSet?
I have search the docs and no results.

It’s a core Java class; or at least I assume it is, since there’s a HashMap, and a Set is just a Map without values. If Java doesn’t actually have a Set class, you can use a Map and just set the value to a placeholder like true.

Thank you for your help, i have made great progress.

Now when i open the activity again with new criteria the data stays the same.
Is there something i need to clear or reset?

Morgan

Can you explain what “new criteria” means here?
Keep in mind that the map function has to be a pure function, i.e. it cannot use any external state including lexically-scoped values from an enclosing function. It has to give the same output every time if given the same input. That means any variable criteria can’t be part of the map function, they have to be used in the query.