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
Highlighted
New Developer
Posts: 13
Registered: ‎02-11-2013
My Device: Simulator :D
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");
}

 

 

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

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
New Developer
Posts: 13
Registered: ‎02-11-2013
My Device: Simulator :D
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 Smiley Tongue)?