Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
nareshkodumuri
Posts: 125
Registered: ‎04-28-2012
My Device: BlackBerry 9800

How to create a listview using xmldatamodel in cpp ....?

Hi,

 

I want to create a listview using XmlDataModel in cpp.I know how to create a listview using xmldatamodel in qml but i am unable to create a listview using xmldatamodel in cpp.Please provide me the code.....

 

 

Regards,

Naresh Kodumuri.

BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer

Re: How to create a listview using xmldatamodel in cpp ....?

Look at cascadescookbookcpp, sheetrecipe

 

Stuart

Developer
raj_jyani
Posts: 107
Registered: ‎05-11-2011
My Device: BlackBerry Z10 LE

Re: How to create a listview using xmldatamodel in cpp ....?

[ Edited ]

Hi ,

If you want simple application then this example helpful for you otherwise go with BlackBerry Development Advisor suggetion.

 

 

XML

 

<?xml version="1.0" encoding="UTF-8" ?>
<persons>
    <person id="1">
        <firstname>John</firstname>
        <surname>Doe</surname>
        <email>john.doe@example.com</email>
        <website>http://aptratech.com</website>
    </person>
    <person id="2">
        <firstname>Jane</firstname>
        <surname>Doe</surname>
        <email>jane.doe@example.com</email>
        <website>http://aptratech.com</website>
    </person>
    <person id="3">
        <firstname>Matti</firstname>
        <surname>Meikäläinen</surname>
        <email>matti.meikalainen@example.com</email>
        <website>http://aptratech.com</website>
    </person>
</persons>

 

 

Cpp Code

 

#include <QObject>
#include <QIODevice>

//#include <QXmlStreamReader>
#include <QtXml/QDomDocument>
#include <bb/cascades/QListDataModel>

#include <bb/cascades/Application>
#include <bb/cascades/ListView>
#include <bb/cascades/GroupDataModel>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

using namespace bb::cascades;

App::App() {
    // Load the QML document and retrieve the root node
    QmlDocument *qml = QmlDocument::create("main.qml");
    AbstractPane *root = qml->createRootNode<AbstractPane>();

    GroupDataModel *model = new GroupDataModel(
            QStringList() << "firstname" << "surname" << "email");
    model->setGrouping(ItemGrouping::ByFirstChar);

    ListView *listView = root->findChild<ListView *>("listView");

    QString appFolder(QDir::currentPath());
    QString fileName = appFolder + "/app/native/assets/models/items.xml";
    fprintf(stderr, "File Path :  %s\n", fileName.toAscii().constData());

    QFile file(fileName);
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
        fprintf(stderr, "Error to open file.\n");
        return;
    } else {
        fprintf(stderr, "File opened.\n");
    }

    QList<QMap<QString, QString> > persons;

    QDomDocument doc("mydocument");
    if (!doc.setContent(&file)) {
        return;
    }

    //Get the root element
    QDomElement docElem = doc.documentElement();

    // you could check the root tag name here if it matters
    QString rootTag = docElem.tagName(); // == persons

    // get the node's interested in, this time only caring about person's
    QDomNodeList nodeList = docElem.elementsByTagName("person");

    //Check each node one by one.
    QMap<QString, QVariant> person;
    for (int ii = 0; ii < nodeList.count(); ii++) {

        // get the current one as QDomElement
        QDomElement el = nodeList.at(ii).toElement();

        //    person["id"] = el.attribute("id"); // get and set the attribute ID

        //get all data for the element, by looping through all child elements
        QDomNode pEntries = el.firstChild();
        while (!pEntries.isNull()) {
            QDomElement peData = pEntries.toElement();
            QString tagNam = peData.tagName();

            if (tagNam == "firstname") {
                //We've found first name.
                person["firstname"] = peData.text();
            } else if (tagNam == "surname") {
                //We've found surname.
                person["surname"] = peData.text();
            } else if (tagNam == "email") {
                //We've found email.
                person["email"] = peData.text();
            } else if (tagNam == "website") {
                //We've found website.
                person["website"] = peData.text();
            }
            pEntries = pEntries.nextSibling();
        }
        model->insert(person);
    }

    listView->setDataModel(model);

    // Set the scene using the root node
    Application::setScene(root);
}

 

 

Qml File

 

import bb.cascades 1.0

Page {
    content: Container {
        ListView {
            id: contactList
            objectName: "listView"
            listItemComponents: [
                ListItemComponent {
                    type: "header"
                    HeaderListItem {
                        title: ListItemData
                    }
                },
                ListItemComponent {
                    type: "item"
                    Container {
                        Label {
                            text: ListItemData.firstname
                        }
                        Label {
                            text: ListItemData.surname
                        }
                        Label {
                            text: ListItemData.email
                        }
                        Container {
                            preferredWidth: 768
                            preferredHeight: 5
                            background: Color.Red
                        }
                    }
                }
            ] // end of listItemComponents list        
        }
    }
}

 

 

Developer
nareshkodumuri
Posts: 125
Registered: ‎04-28-2012
My Device: BlackBerry 9800

Re: How to create a listview using xmldatamodel in cpp ....?

Hi,

 

Thanks for ur reply.Now i got an idea.

 

Regards,

Naresh Kodumuri.