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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 163
Registered: ‎04-28-2012
My Device: BlackBerry 9800
My Carrier: BlackBerry

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.

Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

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

Look at cascadescookbookcpp, sheetrecipe

 

Stuart

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

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        
        }
    }
}

 

 

Highlighted
Developer
Posts: 163
Registered: ‎04-28-2012
My Device: BlackBerry 9800
My Carrier: BlackBerry

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

Hi,

 

Thanks for ur reply.Now i got an idea.

 

Regards,

Naresh Kodumuri.