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
Trusted Contributor
Brennan12325
Posts: 204
Registered: ‎05-15-2012
My Device: None
My Carrier: Telus

QML ListView update item on data model change

[ Edited ]

If I have a list item in QML with a label in it that is made up of two elements of the data... ie.

 Label {
    id: scoreLabel                                        
    text: ListItemData.ups - ListItemData.downs

 The two items that make up the text are subject to change, however.

 

Firstly, how do I change the data model (on button press for example). The code:

ListItemData.ups = ListItemData.ups - 1;

 does not function.

 

Secondly, once I manage to change the data model, how can I have the label recalculate the "text" property. Will it automatically redraw, knowing it's underlying value has changed?

 

I've already implemented this successfully in a C++ list, but  I'm trying really hard not to rewrite a significant amount of QML in C++ just to solve what seems like a minor problem.

 

I guess this problem comes down to how to access and modify the list data model in QML.

----------------------
Check out my app, Alien Flow for reddit

And of course, like my post if you found it helpful or informative!
Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: QML ListView update item on data model change

[ Edited ]

Hi,
You'll have to replace the entire datamodel element. I remember there was an example on this forum but couldn't find it so I hope I got the syntax right:

For GroupDataModel

var updatedItem = ListItemData;
updatedItem.ups = updatedItem.ups - 1;
dataModel.updateItem(indexPath, updatedItem)

For ArrayDataModel:
dataModel.replace(indexPath, updatedItem)

The list items will be updated automatically.

ListItemComponents have their own visibility scope. To access dataModel from them use:
myItem.ListItem.view.dataModel
where myItem is id of top-level item of ListItemComponent.

Example: http://supportforums.blackberry.com/t5/Cascades-Development/Access-ListView-model-from-ListItemCompo...


Andrey Fidrya, @zmeyc on twitter
Please use plain text.