Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
BGmot
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook
My Carrier: x
Accepted Solution

How to get number of elements in ListView from inside QML?

QML/JavaScript newbe question here please.

Suppose I have .qml

 

import bb.cascades 1.0

NavigationPane {
    id: navigationPane
    Page {
...
        Container {
...
              ListView {
                    id: listviewPlayers
...      

 and I want to do something if ListView does not contain any items

    onCreationCompleted: {
        // this slot is called when declarative scene is created
        if (listviewPlayers.AllItems.count() == 0){
            // do something;   
        }

 but when I run this I get complain about AllItems not being defined in this line:

 if (listviewPlayers.AllItems.count() == 0){

What am I doing wron? I can do the same in C++ without any problem.

Thanks.

 

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: How to get number of elements in ListView from inside QML?

What is this AllItems thing? I don't think ids can be capitalized, so that can't be your DataModel's id...

If you were to name your data model, say, dataModel, you should be able to use listviewPlayers.dataModel.childCount([]) as a general approach, or perhaps better use the appropriate method from the DataModel subclass that you're using e.g. dataModel.size() in the case of ArrayDataModel.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
BGmot
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook
My Carrier: x

Re: How to get number of elements in ListView from inside QML?

AllItmes is ListView's attribute: https://developer.blackberry.com/cascades/reference/bb__cascades__listview.html

ListView loads items from DataModel, so I can't directly access ListView's number of (already loaded) items? Instead I have to work with DataModel?

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: How to get number of elements in ListView from inside QML?

Oh, that AllItems thing seems to be a "sentinel" or magic value used in special cases, unrelated to this.

I think ListView merely manages the whole thing, but defers everything to do with the data items to the model, so you do have to go to the model for that data. ListView knows how many VisualNodes (the list item components) it has created at the moment for the onscreen display, but having it also tell you the total number of items in the model would just be duplication. (Interpreting the rationale behind this.)

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
BGmot
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook
My Carrier: x

Re: How to get number of elements in ListView from inside QML?

Changed to         if (listviewPlayers.dataModel.ChildCount([]) == 0){
but getting the error:

file:///apps/.../native/assets//main.qml:108: TypeError: Result of expression 'listviewPlayers.dataModel.ChildCount' [undefined] is not a function.

Please use plain text.
Developer
BGmot
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook
My Carrier: x

Re: How to get number of elements in ListView from inside QML?

Of course childCount() instead of ChildCount()

Thanks Peter.

Please use plain text.