【问题标题】:How to get the list view row count in blackberry 10 cascades qml?如何在黑莓 10 级联 qml 中获取列表视图行数?
【发布时间】:2013-01-03 09:22:30
【问题描述】:

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

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

但即使列表视图行数超过1,它也只显示0。

我的代码

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

【问题讨论】:

  • 我可以给你一个提示:不要使用“+”运算符来连接字符串——你会得到错误的输出——非常奇怪。这样做: QString("%1").arg(number);只是一个旁注:)

标签: blackberry-10 blackberry-cascades


【解决方案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

在主文件中添加以下行

#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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 感谢您的回复,它在按钮单击事件中工作正常,但在 listview 中没有。这里我粘贴了我所做的代码 dataModel: groupdatamodel listItemComponents: [ ListItemComponent { type: "header" Container {首选宽度:748 布局:DockLayout {}标签{文本:“标题”基础:SystemDefaults.TextStyles。 fontWeight: FontWeight.Bold }}Label{ id:subtitle text: groupdatamodel.size()+"Items" textStyle{base:SystemDefaults.TextStyles.SmallText fontWeight: FontWeight.Bold } }
    • 请编辑您的问题并发布格式正确的代码,如果您希望有人查看它。
    【解决方案3】:

    我在我的项目中使用了这段代码,它运行良好:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        你可以使用:

        your_groupdatamodel.size().toString();
        

        your_groupdatamodel.childCount(0);
        

        在 javascript 代码中。

        J.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多