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: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone
Accepted Solution

ListView not populating

Hi all,

 

ListView in my app is not populating, Below is the code snippet

ListView {
                    id:sectionlistview
                    objectName: "sectionlistview"
                    
                    ///dataModel: groupDataModel
                    listItemComponents: [
                        ListItemComponent {
                            id:row
                            type: "item"
                            CustomListItem {
                                dividerVisible: true
                                highlightAppearance: HighlightAppearance.Default
                                Container{
                                    opacity: 0.9
                                    background: Color.create("#c0c0c0")
                                    layout: StackLayout {
                                     orientation: LayoutOrientation.TopToBottom
                                    }
                                    Label {
                                        text: ListItemData.name
                                        textStyle.base: myStyle.style
                                        textStyle.color: Color.create("#ffffff")
                                    }
                                }
                            }
                            
                        }
                    ]
                }           

 

void ApplicationUI::fetchData(){
		listView = root->findChild<ListView*>("sectionlistview");
	    model = new GroupDataModel(QStringList() << "name");
		model->clear();
		model->setGrouping(ItemGrouping::None);
		model->insert(dataName);
		listView->setDataModel(model);
}

 dataName is an QVariantMap variable. Data is available in the variable, i have checked that. 

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: ListView not populating

Have you checked if findChild is finding the object?

 

Also is your data within the first two levels and / or the indexPath set appropriately?


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: ListView not populating

The app would crash I think if findChild would fail. Have you tried using a static XML in QML to make sure your ListView is properly configured?

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Highlighted
Retired
Posts: 571
Registered: ‎06-25-2010
My Device: Z10
My Carrier: Vodafone

Re: ListView not populating

The following code, almost identical to yours works for me. There are a few things in your code I cannot see so it's hard to check every detail. First, create a new app and overwrite applicationui.cpp, applicationui.hpp and main.qml with my code and then test it. If it works, check your code against mine. If it doesn't, let me know the device and OS version you are testing on. 

 

#ifndef ApplicationUI_HPP_
#define ApplicationUI_HPP_

#include <QObject>

namespace bb
{
    namespace cascades
    {
        class Application;
        class LocaleHandler;
    }
}

class QTranslator;

/*!
 * @brief Application object
 *
 *
 */

class ApplicationUI : public QObject
{
    Q_OBJECT
public:
    ApplicationUI(bb::cascades::Application *app);
    virtual ~ApplicationUI() { }
    Q_INVOKABLE void fetchData();

private slots:
    void onSystemLanguageChanged();
private:
    QTranslator* m_pTranslator;
    bb::cascades::LocaleHandler* m_pLocaleHandler;

};

#endif /* ApplicationUI_HPP_ */

applicationui.hpp

 

 

#include "applicationui.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>
#include <bb/cascades/ListView>
#include <bb/cascades/GroupDataModel>

using namespace bb::cascades;

static AbstractPane *root = 0;

ApplicationUI::ApplicationUI(bb::cascades::Application *app) :
        QObject(app)
{
    // prepare the localization
    m_pTranslator = new QTranslator(this);
    m_pLocaleHandler = new LocaleHandler(this);

    bool res = QObject::connect(m_pLocaleHandler, SIGNAL(systemLanguageChanged()), this, SLOT(onSystemLanguageChanged()));
    // This is only available in Debug builds
    Q_ASSERT(res);
    // Since the variable is not used in the app, this is added to avoid a
    // compiler warning
    Q_UNUSED(res);

    // initial load
    onSystemLanguageChanged();

    // Create scene document from main.qml asset, the parent is set
    // to ensure the document gets destroyed properly at shut down.
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    // Create root object for the UI
    root = qml->createRootObject<AbstractPane>();

    // Set created root object as the application scene
    app->setScene(root);

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

}

void ApplicationUI::onSystemLanguageChanged()
{
    QCoreApplication::instance()->removeTranslator(m_pTranslator);
    // Initiate, load and install the application translation files.
    QString locale_string = QLocale().name();
    QString file_name = QString("SupportListView_%1").arg(locale_string);
    if (m_pTranslator->load(file_name, "app/native/qm")) {
        QCoreApplication::instance()->installTranslator(m_pTranslator);
    }
}

void ApplicationUI::fetchData()
{

    QVariantMap dataName;
    dataName["name"] = "Martin";
    ListView* listView = root->findChild<ListView*>((const QString) "sectionlistview");
    GroupDataModel* model = new GroupDataModel(QStringList() << "name");
    model->clear();
    model->setGrouping(ItemGrouping::None);
    model->insert(dataName);
    listView->setDataModel(model);
}

 applicationui.cpp

 

 

import bb.cascades 1.2

Page {
    Container {
        //Todo: fill me with QML
        Label {
            // Localized text with the dynamic translation and locale updates support
            text: qsTr("Hello World") + Retranslate.onLocaleOrLanguageChanged
            textStyle.base: SystemDefaults.TextStyles.BigText
        }
        Button {
            text: "Fetch Data"
            onClicked: {
                app.fetchData();
            }
        }
        ListView {
            id: sectionlistview
            objectName: "sectionlistview"

            ///dataModel: groupDataModel
            listItemComponents: [
                ListItemComponent {
                    id: row
                    type: "item"
                    CustomListItem {
                        dividerVisible: true
                        highlightAppearance: HighlightAppearance.Default
                        Container {
                            opacity: 0.9
                            background: Color.create("#c0c0c0")
                            layout: StackLayout {
                                orientation: LayoutOrientation.TopToBottom
                            }
                            Label {
                                text: ListItemData.name
                                textStyle.base: myStyle.style
                                textStyle.color: Color.create("#ffffff")
                            }
                        }
                    }

                }
            ]
        }
    }
}

 main.qml

 

Good luck!

 

Martin

 

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: ListView not populating

@zezke it would but as you see all the code has obviously not been posted here so just for completeness I included this as he may be referencing a different object accidentally.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: ListView not populating

When i placed the function (populating to listview) after QNetworkReply, the list loading properly.

//////////////////////////////////////////////////////Before/////////////////////////////////////////////
QNetWorkRequest call
function for populating the list

function for QNetworkReply{
}

function for Populating the list{}

////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////Now///////////////////////////////////////////
QNetWorkRequest call


function for QNetworkReply{
function for populating the list
}

function for Populating the list{}

/////////////////////////////////////////////////////////////////

Thanks for the help.
Retired
Posts: 571
Registered: ‎06-25-2010
My Device: Z10
My Carrier: Vodafone

Re: ListView not populating

OK, so you're saying that your ListView was being populated with data retrieved from the network but you were inserting it into the data model before you got your reply? Makes sense.

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
New Contributor
Posts: 8
Registered: ‎11-18-2013
My Device: Dev Alpha
My Carrier: GSM

Re: ListView not populating

[ Edited ]

Very helpful question, and the fancy answer quenched my headaches. I am also confronted with a listview populating trouble. I feel so fortunate to get so many hints here.