如何在 Blackberry Cascades 中创建用于 JSON 解析的列表视图

how to create listview for json parsing in blackberry cascades

本文关键字:JSON 视图 列表 用于 创建 Blackberry Cascades      更新时间:2023-10-16
  1. 我创建了 json 网络服务。
  2. 我在 cpp 文件中得到了响应
  3. 如何在 qml 页面中显示收到的 json 数据
  4. ListView *listView = root->findChild<ListView*>("listView");显示根在范围内未被划分
  5. 请告诉如何将列表绑定到主应用程序中

我的代码在这里


1. QML 文件

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 文件

// 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文件

// 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();
        }
    }

查询:

列表视图无法创建

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,并从 cpp 引用它。然后,您可以将整个地址映射附加到 ArrayDataModel,然后根据需要在项组件中引用键。

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