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
Posts: 64
Registered: ‎06-28-2011
My Device: BlackBerry Z10
My Carrier: Bell

Saving checkbox selection

Hi,

 

I have a listview that contains a custom list item that contains an image and a checkbox. I need the selection of the checkboxes to be saved after the app is closed.

 

I have tried using Qsettings, but I am fairly new to developing and don't know if I have done it correctly.

 

main.qml

                                    CheckBox {
                                        id: checkbox
                                        objectName: "checkbox"
                                        verticalAlignment: VerticalAlignment.Center
                                        horizontalAlignment: HorizontalAlignment.Right
                                        checked: _rssnewsApp.getValueFor(objectName, "yes") == "true" ? true : false
                                        onCheckedChanged: {
                                            _rssnewsApp.saveValueFor(checkbox.objectName, checkbox.checked)
                                        }

                                    }

 rssnews.cpp

#include "rssnews.h"

#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>
#include <bb/cascades/QmlDocument>
#include <QSettings>

using namespace bb::cascades;

rssnews::rssnews()
{
    
    // when saving values to the persistent store.
    QCoreApplication::setOrganizationName("Example");
    QCoreApplication::setApplicationName("rssnews");

    
    mTranslator = new QTranslator(this);
    mLocaleHandler = new LocaleHandler(this);
    connect(mLocaleHandler, SIGNAL(systemLanguageChanged()), SLOT(onSystemLanguageChanged()));
    onSystemLanguageChanged();

    
    QmlDocument *qml = QmlDocument::create("asset:///main.qml");
    qml->setContextProperty("_rssnewsApp", this);

    if (!qml->hasErrors()) {
        AbstractPane *appPane = qml->createRootObject<AbstractPane>();
        if (appPane) {
            Application::instance()->setScene(appPane);
        }
    }
}

rssnews::~rssnews()
{
}

QString rssnews::getValueFor(const QString &objectName, const QString &defaultValue)
{
    QSettings settings;

    
    if (settings.value(objectName).isNull()) {
        return defaultValue;
    }

    
    return settings.value(objectName).toString();
}

void rssnews::saveValueFor(const QString &objectName, const QString &inputValue)
{
    
    QSettings settings;
    settings.setValue(objectName, QVariant(inputValue));
}

void rssnews::onSystemLanguageChanged()
{
   
    QCoreApplication::instance()->removeTranslator(mTranslator);

    
    QString localeString = QLocale().name();
    QString fileName = QString("rssnewssettings_%1").arg(localeString);
    if (mTranslator->load(fileName, "app/native/qm")) {
        QCoreApplication::instance()->installTranslator(mTranslator);
    }
    qDebug() << "language " << localeString;
}

 

Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Saving checkbox selection

I think you need to update the model in which your listview is based...

Developer
Posts: 64
Registered: ‎06-28-2011
My Device: BlackBerry Z10
My Carrier: Bell

Re: Saving checkbox selection

As in add a "checkbox" aspect in the JSON file?
Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Saving checkbox selection

Yes.
The only problem here is that you have to play the json file in a Read/Write folder in your app...
I think it's better to built a model in memory, updates it as the user changes the checkbox and then when your are ready save them to the json file.
Using a database could be cleaner...

Developer
Posts: 64
Registered: ‎06-28-2011
My Device: BlackBerry Z10
My Carrier: Bell

Re: Saving checkbox selection

Is there any documentation that I should look at?

Thanks for the responses.
Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Saving checkbox selection

You could check the AddressBook sample (http://blackberry.github.io/Cascades-Samples/addressbook.html) and the Account sample (http://blackberry.github.io/Cascades-Samples/accounts.html).

In those samples the model is updated and the Listview gets refreshed. They do not use a database but can give you an idea how to start... 

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Saving checkbox selection

i think the main issue here is the context of the listview.
you cannot directly access a context variable of the main document from a listitemcomponent.

check your code from a component outside the list, if it works you confirmed the issue and can use a workaround (enter it into the search to find more about it).
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter