0

I am getting JSON data from the web service below:

"List1": [
        {
            "id": "1",
            "title": "Title1",
            "picture":"myURL"
        },
        {
            "id": "2",
            "title": "Title2",
            "picture":"myURL" 
        }
]

Now, I want to create a custom ListView using this JSON data, with the title and image.

I have tried many examples for this. Some of links are given below:

http://qt-project.org/wiki/JSONListModel http://developer.blackberry.com/native/documentation/cascades/device_platform/data_access/using_data_source.html

But, I still cannot find any solutions. Can you please help me to solve this issue?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179

1 Answers1

1

Creating a list is simple enough task. It's a bit harder because you want to show image from the internet, so you have to use a custom class for this. Download WebImageView.cpp and WebImageView.h and add them inside /src directory (if you want to take a look at the whole project or just follow my steps).

Add the following inside applicationui.cpp to include new class

#include "WebImageView.h"

and inside ApplicationUI(bb::cascades::Application *app) add

qmlRegisterType<WebImageView>("org.labsquare", 1, 0, "WebImageView");

so your QML can access this class.

This is a complete working sample of QML:

import bb.cascades 1.2
import bb.data 1.0
import org.labsquare 1.0

NavigationPane {
    id: nav
    Page {
        Container {
            ListView {
                dataModel: dataModel 
                listItemComponents: [
                    ListItemComponent {
                        type: "item"
                        content: Container {
                            Label {
                                text: ListItemData.title
                            }
                            WebImageView {
                                url: "http://adev.si/files/"+ListItemData.picture
                            }
                        }
                    }
                ]
                attachedObjects: [
                    GroupDataModel {
                        id: dataModel
                        grouping: ItemGrouping.None
                    },
                    DataSource {
                        id: dataSource
                        source: "http://adev.si/files/someData.json"
                        remote: true
                        onDataLoaded: {
                            dataModel.insertList(data.List1)
                        }
                    }
                ]
            }
        }
    }
    onCreationCompleted: {
        dataSource.load();
    }
}

I hope this helped. You also need this inside your .pro file

LIBS   += -lbbdata
QT += network

If you want to you can import this project and use it.

Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57
  • Hey, thank you for the answer. It works great. I really appreciate your help. But there is one thing which i want to know that how can i pass POST method instead of GET method in DataSource?? Because we use POST method in our project. So, please let me know ASAP. – user3767332 Jun 24 '14 at 04:40
  • I don't think Cascades has built in POST anywhere, so try and use `XMLHttpRequest()`, which is JavaScript function. – Bojan Kogoj Jun 24 '14 at 09:47
  • OK. Thanks. So, please can you give me any example if you have for the POST method using `XMLHttpRequest()`, also if you want i can provide you my URL which i am using... Thank you once again for your all kind of help. – user3767332 Jun 24 '14 at 09:59