如何在黑莓10 cascades qml中获取列表视图行数?

时间:2013-01-03 09:22:30

标签: blackberry-10 blackberry-cascades

我需要在列表视图中获取行数no black 10 cascades qml?列表视图数据源模型类型为json。我试过这个

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

但它的列表视图行数仅显示0,大于1。

我的代码

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

5 个答案:

答案 0 :(得分: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

  4. 在主档

    中添加以下行
    #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

答案 1 :(得分:0)

DataModel :: childCount(ListItem indexPath)返回indexPath指定的列表中项目的子计数,而不是数据模型中数据项的计数(因此可用于列表)。您需要询问实际的数据模型。例如,GroupDataModel :: size()返回GroupDataModel中的项目数,类似于QListDataModel。

答案 2 :(得分:0)

我在我的项目中使用此代码并且工作正常:

 console.log("pcs count" + pcsListModel.childCount(0));

答案 3 :(得分:0)

除非您使用自定义数据模型,否则无法在标签中添加自定义元素。

答案 4 :(得分:0)

你可以使用:

your_groupdatamodel.size().toString();

your_groupdatamodel.childCount(0);

在javascript代码中。

学家