user2580026
user2580026

Reputation: 13

How can i load data and display it using CouchBase Lite

I'm a in the process of understanding CouchBase Lite for android application,as far i only know how to create a manager, database, document and view.However i want to load data from the documents and show it int the app but failed after trying several times and following some tutorials.Is there a way i can load this data and manipulate it? The code i used til now is the following:

        // create a manager
        Manager manager;
        try {
            manager = new Manager(new AndroidContext(this), Manager.DEFAULT_OPTIONS);
        } catch (IOException e) {
            getFilesDir();
            return;
        }
        // create a name for the database and make sure the name is legal
        String app4 = "list";
        if (!Manager.isValidDatabaseName(app4)) {
            return;
        }
// create a new database

        Database database;
        try {
            database = manager.getDatabase(app4);
            Log.d (TAG, "Database created");


        } catch (CouchbaseLiteException e) {
            Log.e(TAG, "Cannot get database");
            return;
        }

        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("title", " Au bonheur des dames");
        properties.put("author", "Emile ZOla");

//        Document document = database.createDocument();
//        Document retrievedDocument = database.getDocument(documentID);
        Document document = database.createDocument();

        try {
            document.putProperties(properties);
        } catch (CouchbaseLiteException e) {
            Log.e(TAG, "Cannot save document", e);
        }

        Query query = database.createAllDocumentsQuery();

        try {
            QueryEnumerator queryEnumerator = query.run();
            List<QueryRow> results = new ArrayList<QueryRow>();

            for (int i=0; i< queryEnumerator.getCount(); i++) {
                results.add(queryEnumerator.getRow(i));
            }

If anyone could help please, i would be very thankful.As i said i am very new to Couchbase and thus may commit a lot of mistakes...

Upvotes: 0

Views: 437

Answers (1)

sweetiewill
sweetiewill

Reputation: 568

Have an in-depth look into map/reduce techniques to create an 'View,' which is an index of documents in the database and from there 'Query' to look up the result of the index that the View provides.

Going over the Couchbase Mobile mini-hack tutorial and specifically focus on steps 3-5 and 8 would help with further understanding on how to load and display data.

Upvotes: 1

Related Questions