0

I need to get the no of rows in a list view blackberry 10 cascades qml? The list view data source model type is json. I have tried this

ListItem.initialized ? ListItem.view.dataModel.childCount(ListItem.indexPath) : 0

But its displaying only 0 even the list view row count is more than 1.

My code

dataModel: groupdatamodel 
listItemComponents: [ 
    ListItemComponent { 
        type: "header" 

        Container { 
            preferredWidth: 748 
            layout: DockLayout { }

            Label {
                text: "Title" 
                base: SystemDefaults.TextStyles.TitleText
                fontWeight: FontWeight.Bold
            }
        }

        Label { 
            id: subtitle 
            text: groupdatamodel.size() + "Items"

            textStyle { 
                base: SystemDefaults.TextStyles.SmallText 
                fontWeight: FontWeight.Bold
        } 
    }
]
CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
SelvaRaman
  • 218
  • 2
  • 15
  • One tip I can give you: Don't use the "+" operator to concatenate strings - you'll get mangled output - very weird. Do it like this: QString("%1").arg(number); Just a side note :) – Quintin Balsdon Jun 15 '13 at 21:38

5 Answers5

1
  1. Main.qml

    import bb.cascades 1.0
    import bb.data 1.0
    
    Page {
        content: Container {
            Label {
                text: "List View with json parsing"
            }
            ListView {
                id: listViewDemo
                dataModel: GroupDataModel {
                    grouping: ItemGrouping.None
                }
                listItemComponents: [
                    ListItemComponent {
                        type: "listItem"
                        StandardListItem {
                            title: ListItemData.ThumnailImage
                            description: ListItemData.CategoryID
                        }
                    }
                ]
                function itemType(data, indexPath) {
                    return "listItem";
                }
            }
        }
        attachedObjects: [
            DataSource {
                id: serviceDataSource
                source: "contacts.json"
                type: DataSourceType.Json
                onDataLoaded: {
                    listViewDemo.dataModel.clear();
                    listViewDemo.dataModel.insertList(data)
                }
            }
        ]
        onCreationCompleted: {
            serviceDataSource.load();
        }
    }
    
  2. Contacts.json

    [   {"CategoryID":"3","CategoryName":"News","CountryID":"1","Result":"OK"},
    

    {"CategoryID":"4","CategoryName":"Daily Paper","CountryID":"1","Result":"OK"},{"CategoryID":"5","CategoryName":"Thanthi","CountryID":"1","Result":"OK"}, {"CategoryID":"1","CategoryName":"Newspaper","CountryID":"1","Result":"OK"}, {"CategoryID":"2","CategoryName":"Magazine","CountryID":"1","Result":"OK"} ]

  3. main.cpp

add follwing lines in main file

#include <bb/data/DataSource>
#include <bb/data/JsonDataAccess>

Q_DECL_EXPORT int main(int argc, char **argv)
{
    // We want to use DataSource in QML
    bb::data::DataSource::registerQmlTypes();

4.FILENAME.PRO

LIBS += -lbbdata

nomistic
  • 2,902
  • 4
  • 20
  • 36
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
0

DataModel::childCount(ListItem indexPath) returns the child count for the item in the list specified by indexPath, not the count of data items in the data model (and therefor available to the list). You need to interrogate the actual data model. For example GroupDataModel::size() returns the number of items in the GroupDataModel, similarly for QListDataModel.

Richard
  • 8,920
  • 2
  • 18
  • 24
  • Thanks for your reply, Its working fine in Button click event but not in listview.Here i have pasted the code what i have done dataModel: groupdatamodel listItemComponents: [ ListItemComponent { type: "header" Container { preferredWidth: 748 layout: DockLayout {} Label { text: "Title" base:SystemDefaults.TextStyles. fontWeight: FontWeight.Bold }}Label{ id:subtitle text: groupdatamodel.size()+"Items" textStyle{base:SystemDefaults.TextStyles.SmallText fontWeight: FontWeight.Bold } } – SelvaRaman Jan 06 '13 at 06:33
  • Please edit your question and post the code, properly formatted, there if you want someone to look at it. – Richard Jan 06 '13 at 14:18
0

I use this code in my project and it works fine:

 console.log("pcs count" + pcsListModel.childCount(0));
Hisham Bakr
  • 559
  • 4
  • 13
0

Adding custom elements in label is not possible unless you use custom data model.

0

you can use:

your_groupdatamodel.size().toString();

or

your_groupdatamodel.childCount(0);

in javascript code.

J.