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
Developer
Posts: 38
Registered: ‎12-06-2011
My Device: Playbook 64GB, Curve 9360, BB Z10 LE
My Carrier: Koodo
Accepted Solution

Trouble Populating ListView

Hi,

 

I am having trouble inserting data from a QVariantList into a ListView using a GroupDataModel. The app works, but the list is empty. I have confirmed that the QVariantList is not empty. I have been struggling with this for some time and would appreciate any help.

 

 


 

From the app constructor :

 

mDb = new databaseHandler();

mQml = QmlDocument::create("asset:///main.qml");
//-- setContextProperty expose C++ object in QML as an variable
if (!mQml->hasErrors()) {
mQml->setContextProperty("app", this);
mTimer = new QTimer(this);
connect(mTimer, SIGNAL(timeout()), this, SLOT(update()));

mRoot = mQml->createRootObject<AbstractPane>();
loadData();
Application::instance()->setScene(mRoot);

//int i = 9;
}
else
{
QList<QDeclarativeError> error = mQml->errors();
}

 


 

void App::loadData()
{
QString databaseName = "timetracker_test_data.db";
mTimeEntries = mDb->loadDataBase(databaseName);
GroupDataModel *mDataModel = mRoot->findChild<GroupDataModel*>("timeEntryModel");

QVariantList sqlData;

for(int count = 0; count < mTimeEntries.count(); count ++)
{
QVariantMap timeEntryMap;
timeEntryMap["description"] = mTimeEntries[count].getDescription();
int time = mTimeEntries[count].getTotalTime();

timeEntryMap["time"] = intTimeToString(time);
sqlData.append(timeEntryMap);
//mRoot->setProperty("currTimeText", mTimeEntries[count].getDescription());
mDataModel->insert(timeEntryMap);


}

mDataModel->insertList(sqlData);
}

 


 

From main.qml : 

 

ListView {
id: listContainer
property variant itemTextStyle: defaultBlackTextStyle
dataModel: timeEntryModel

listItemComponents: [
ListItemComponent {
type: "TimeEntry"
TimeEntry {
}
}
]
attachedObjects: [
// A GroupDataModel is used for presenting the SQL data. All set up is done here
// except for the actual population of the model, which is done in C++.
GroupDataModel {
id: timeEntryModel
objectName: "timeEntryModel"
}
]
}

 


 

and from TimeEntry.qml :

Container {
layout: StackLayout {

}
Label {
// The title is bound to the data in models/recipemodel.xml title attribute.
text: ListItemData.description;
textStyle {
base: SystemDefaults.TextStyles.TitleText
color: Color.Black
}
}
Label {
// The title is bound to the data in models/recipemodel.xml title attribute.
text: ListItemData.time;
rightMargin: 25.0
leftMargin: 25.0
layoutProperties: StackLayoutProperties {

}
rotationZ: 0.0
textStyle {
base: SystemDefaults.TextStyles.TitleText
color: Color.Black
}
}
}

 

Regular Contributor
Posts: 76
Registered: ‎07-31-2012
My Device: Blackberry
My Carrier: Vodafone

Re: Trouble Populating ListView

Try inserting data like this:

 

QVariantList qvList = mTimeEntries.toList();

for (QList<QVariant>::iterator it = qvList.begin(); it != qvList.end();
it++) {
mDataModel->insert(it->toMap());
}

 

& access data in qml as follows:

text: ListItemData.SQLFieldName

 

Hope it helps.

 

Regards,

Nishant Shah

Developer
Posts: 38
Registered: ‎12-06-2011
My Device: Playbook 64GB, Curve 9360, BB Z10 LE
My Carrier: Koodo

Re: Trouble Populating ListView

Thanks Nishant,

 

Unfortunately this approach does not solve the problem and the list is still empty.

Developer
Posts: 292
Registered: ‎07-25-2008
My Device: Passport

Re: Trouble Populating ListView

Try changing the list item type to "item", or simply removing the list item type entirely.  The type parameter is only useful if you're explicitly customizing the list item type mapping, and causes problems otherwise.

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Trouble Populating ListView

Your problem is that you are not overriding the itemType function in the listview qml so the listview doesnt know which component to give to each item.

 

function itemType(data, indexPath) { } or something like that, the actual method is in the docs somewhere

Like all of my posts
Developer
Posts: 38
Registered: ‎12-06-2011
My Device: Playbook 64GB, Curve 9360, BB Z10 LE
My Carrier: Koodo

Re: Trouble Populating ListView

Thanks for the replies on this one all. Turns out I was diagnosing the incorrect issue all along. Turns out my listview was being populated from the beginning but a UI issue was causing my listview to be hidden by another component. (don't ask why I never checked this at first) The nice thing about this "non-issue" is that I now know how the listview works inside and out as I tried every conceivable method to solve this

 

Gerry