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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Cascades Development

Reply
New Developer
matshy
Posts: 13
Registered: ‎02-11-2013
My Carrier: Plus GSM

Real time ListView refreshing

Hello again Dear Developers!

 

I am wondering, it is possible to display every change in content of my list immediately after it introduction?

I mean - I have list (XMLListModel) and I fill each record of list by the textfield. Everything  works great but my entries are visible after relaunch application. So it will be great if every change would be display immediately. Can you give me any solutions?

 

Greetings

 

My code(main.qml):

 

// Default empty project template
import bb.cascades 1.0

// creates one page with a label
Page {
    Container {
        
        layout: StackLayout {}
        Label {
            id: label
            text: "Test app"
            textStyle.base: SystemDefaults.TextStyles.BigText
        }
        TextField {
            id: textfield
            hintText: "Enter Text..."
        }
        Button {
            text: "Add"
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            onClicked: {
                mainObj.add(textfield.text)
            }
        }
        Button {
            text: "Save"
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            onClicked: {
                mainObj.save()
            }
        }
        
        ListView {
            dataModel: XmlDataModel {
                source: mainObj.sciezka()
            }
            listItemComponents: [
                ListItemComponent {
                    type: "listItem"
                    Container {
                        layout: StackLayout {
                            orientation: LayoutOrientation.LeftToRight
                        }
                        CheckBox {
                            checked: ListItemData.checked
                        }
                        Label {
                            text: ListItemData.task
                            textStyle {
                                base: SystemDefaults.TextStyles.TitleText
                                fontWeight: FontWeight.Normal
                            }
                        }
                    } 
                } 
            ]
        }
    }
}

 

and the content of cpp file:

 

// Default empty project template
#include "Test.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

#include <QFile>
#include <QTextStream>
#include <QtXml>

using namespace bb::cascades;

QFile file(QDir::homePath()+"/items.xml");
QDomDocument baza;
QDomElement korzen = baza.createElement("root");

Test::Test(bb::cascades::Application *app)
: QObject(app)
{
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    baza.appendChild(korzen);

    qml->setContextProperty("mainObj",this);

    AbstractPane *root = qml->createRootObject<AbstractPane>();
    app->setScene(root);
}

void Test::add(QString data){
	QDomElement element = baza.createElement("listItem");
	element.setAttribute("task", data);
	korzen.appendChild(element);
}


void Test::save(){
	if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
		QTextStream stream(&file);
	    stream << baza.toString();
	    file.close();
	}
}

QString Test::sciezka() {
	return (QDir::homePath()+"/items.xml");
}

 

 

Please use plain text.
Developer
Zmey
Posts: 982
Registered: ‎12-18-2012

Re: Real time ListView refreshing

[ Edited ]

XMLDataModel items can't be changed once they are loaded. This model is very limited and is good only for prototyping things.

To reload ListView data while using XMLDataModel save the changes to disk then reset dataModel's source property. But this is very inefficient.

I think it's better to switch to ArrayDataModel or, if sorting is needed, to GroupDataModel. Parse XML manually using XMLDataAccess class and insert the data into GroupDataModel. This page has sample code for this:

http://developer.blackberry.com/cascades/documentation/device_platform/data_access/working_with_xml....

This way you can modify dataModel's entries, add new ones etc and the changes will be automatically reflected in ListView. DataModel emits signals when data changes and ListView reacts to them.

It's also possible to create a custom DataModel subclass.


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
New Developer
matshy
Posts: 13
Registered: ‎02-11-2013
My Carrier: Plus GSM

Re: Real time ListView refreshing

Thank ypu a lot for your quick reply.

But I am a beginer in Qt and especially in Cascades. I am writing my first app, so can you give me some solution or a little bit code example how use a GroupDataModel or ArrayDataModel to auto reload items (version for dummies please :smileytongue:)?

Please use plain text.