Rajesh Loganathan
Rajesh Loganathan

Reputation: 11247

how to create listview for json parsing in blackberry cascades

  1. Am created json webservice.
  2. I got response in cpp file
  3. How to show received json data in qml page
  4. ListView *listView = root->findChild<ListView*>("listView"); shows root was not declated in scope
  5. Please tell how to bind list into main app

My code is here


1. QML FILE

import bb.cascades 1.0
import bb.data 1.0

Page {
    content: Container {
        layout: StackLayout {
            orientation: LayoutOrientation.TopToBottom

        }

        TextField  {
            id: countryID
            hintText: "Enter Country ID  eg:'1'"  
            maxWidth: 400
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            textStyle.textAlign: TextAlign.Center

        }


       Button {
           id: btn

           text: "Send JSON Request"
           onClicked: {
               app.sendRequest(countryID.text);

           }
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
        }

       ListView {
           objectName: "listView"
           listItemComponents: [
               ListItemComponent {
                   StandardListItem {
                       id: item
                       title: ListItemData.CategoryName
                       description: ListItemData.CategoryID
                   }
               }
           ]
       }



}   //end of container      

}   //end of page

2. HPP FILE

// Default empty project template
#ifndef CALCI_HPP_
#define CALCI_HPP_

#include <QObject>
#include <bb/cascades/QListDataModel>


namespace bb { namespace cascades { class Application; class ListView; }}

class controller : public QObject
{
    Q_OBJECT

public:
    controller(bb::cascades::Application *app);

public Q_SLOTS:
    void sendRequest(const QString &countryID);

private Q_SLOTS:

    void onFinished();

};


#endif

3. CPP FILE

// Default empty project template
#include "calci.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/ListView>
#include <bb/cascades/ArrayDataModel>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QUrl>
#include <bb/data/JsonDataAccess>

using namespace bb::cascades;
using namespace bb::data;

controller::controller(bb::cascades::Application *app)
: QObject(app)
{

    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("app", this);

    AbstractPane *root = qml->createRootObject<AbstractPane>();

    app->setScene(root);
}


void controller::sendRequest(const QString &countryID)
{

    QNetworkAccessManager* networkAccessManager = new QNetworkAccessManager(this);

    const QString queryUri = QString::fromLatin1("http://192.168.1.251:410/Mobile/Service1.svc/english/Category?CountryID=%1").arg(countryID);

    QNetworkRequest request(queryUri);

    QNetworkReply* reply = networkAccessManager->get(request);

    bool ok = connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
    Q_ASSERT(ok);
    Q_UNUSED(ok);
}

void controller::onFinished()
{
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
    ArrayDataModel *model = new ArrayDataModel();

    QString response;
    if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200)
    {
            JsonDataAccess jda;
            QVariantMap map = jda.loadFromBuffer(reply->readAll()).toMap();

            QVariantList addresses = map["GetCategoryResult"].toList();

            foreach(QVariant var, addresses) {
                QVariantMap addressMap = var.toMap();

                qDebug() << "CategoryName is " << addressMap["CategoryName"].toString();
                qDebug() << "CategoryID is " << addressMap["CategoryID"].toString();
                qDebug() << "CategoryID is " << addressMap["ThumnailImage"].toUrl();

                model->append(addressMap);

            }

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

        }
        else {
            qDebug() << "Server returned code " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        }
    }

QUERY :

List view cant able to create

qDebug() << "CategoryName is " << addressMap["CategoryName"].toString();
        qDebug() << "CategoryID is " << addressMap["CategoryID"].toString();
        qDebug() << "CategoryID is " << addressMap["ThumnailImage"].toUrl();

        model->append(addressMap);

    }

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

Upvotes: 2

Views: 1594

Answers (1)

Scott
Scott

Reputation: 552

You should create your ListView in QML, and reference it from cpp. You can then append the whole addressMap to your ArrayDataModel, and then reference the keys as needed in your item component.

CPP:

foreach(QVariant var, addresses) {
    QVariantMap addressMap = var.toMap();
    qDebug() << "CategoryName is " << addressMap["CategoryName"].toString();
    qDebug() << "CategoryID is " << addressMap["CategoryID"].toString();
    qDebug() << "CategoryID is " << addressMap["ThumnailImage"].toUrl();
    model->append(addressMap);

}
ListView *listView = root->findChild<ListView*>("listView");`
listView->setDataModel(model);

QML:

    ListView {
        objectName: "listView"
        listItemComponents: [
            ListItemComponent {
                StandardListItem {
                    id: item
                    title: ListItemData.CategoryName
                    imageSource: "asset:///images/" + ListItemData.ThumnailImage // if the image is local
                }
            }
        ]
    }

Upvotes: 3

Related Questions