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

Posts: 33
Registered: ‎01-12-2011
My Device: Torch
Accepted Solution

How to handle updates to data in ArrayDataModel


I load data from a JSON file into a QVariantList which is then appended to an ArrayDataModel which is used to show the data in a ListView. This all works fine. I now want to update some values from a ListViewItem and want to propagate those changes back to the ArrayDataModel -> QVariantList -> JSON file. How would i handle that? Should i update the ListView or the DataModel or even the QVariantList? Loading and showing data in a ListView is very well documented but i can find litle information on the other way arround?


Any help will be greatly appreciated!

Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: How to handle updates to data in ArrayDataModel

[ Edited ]

You can access the dataModel from ListView items so it's possible to manipulate it directly.


Outside of ListView subscribe to dataModel's signals such as

void	itemAdded (QVariantList indexPath)
void	itemUpdated (QVariantList indexPath)
void	itemRemoved (QVariantList indexPath)
void	itemsChanged (bb::cascades::DataModelChangeType::Type eChangeType=bb::cascades::DataModelChangeType::Init, QSharedPointer< bb::cascades::DataModel::IndexMapper > indexMapper=QSharedPointer< bb::cascades::DataModel::IndexMapper >(0))

You'll probably won't have to subscribe to all of them as you know which changes you're making to the dataModel. For example, if you're only updating the items subscribe to itemUpdated etc.


In signal handler fetch the changes from dataModel and save them to disk.


p.s. Be sure to subscribe to changes only after the dataModel is initially populated.


Andrey Fidrya, @zmeyc on twitter
Posts: 33
Registered: ‎01-12-2011
My Device: Torch

Re: How to handle updates to data in ArrayDataModel

OK thank you for those pointers. I now have a working solution where i save the currentIndex of the selected list item. If it needs to be changed i call the following code:


QVariantMap item = modelItems->value(index).toMap();
item.insert("attribute", newValue);
modelItems->replace(index, item);


That triggers the datamodel signals which i use to save the data to the filesystem.