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
New Developer
Posts: 4
Registered: ‎05-21-2012
My Device: Dev Alpha
My Carrier: Meteor

Linking a GroupDataModel to ListView

Hi there,

 

I'm developing a Cascades application that consumes an XML feed of live transport information. It involves presenting information from the feed in a ListView. Unfortunately, while I can connect the data model to the list view, I don't seem to be able to access the items in the model using the ListItemData.name syntax.

 

I have a class that inherits from GroupDataModel. Inside the class, I parse the XML feed and create a QVariantMap containing the relevant keys and values.

 

This is what the QVariantMap looks like when I print it with qDebug():

 

QMap(("Destination", QVariant(QString, "Howth") ) ( "Destinationtime" ,  QVariant(QString, "23:04") ) ( "Direction" ,  QVariant(QString, "Northbound") ) ( "Duein" ,  QVariant(QString, "10") ) ( "Exparrival" ,  QVariant(QString, "22:18") ) ( "Expdepart" ,  QVariant(QString, "22:18") ) ( "Lastlocation" ,  QVariant(QString, "Arrived Dalkey") ) ( "Late" ,  QVariant(QString, "0") ) ( "Locationtype" ,  QVariant(QString, "S") ) ( "Origin" ,  QVariant(QString, "Bray") ) ( "Querytime" ,  QVariant(QString, "22:08:55") ) ( "Scharrival" ,  QVariant(QString, "22:18") ) ( "Schdepart" ,  QVariant(QString, "22:18") ) ( "Servertime" ,  QVariant(QString, "2012-05-21T22:08:55.377") ) ( "Stationcode" ,  QVariant(QString, "SEAPT") ) ( "Stationfullname" ,  QVariant(QString, "Seapoint") ) ( "Status" ,  QVariant(QString, "En Route") ) ( "Traincode" ,  QVariant(QString, "E937 ") ) ( "Traindate" ,  QVariant(QString, "21 May 2012") ) ( "Traintype" ,  QVariant(QString, "DART") ) )

 


 

Every time I have parsed a section of XML, I insert the QVariantMap into the GroupDataModel as follows:

 

this->insert(parseStation(xml));

 


 

where parseStation() is a method that returns a QVariantMap and xml is a QXmlStreamReader object.

 

Elsewhere, I connected the data model to the ListView like so:

 

ListView *trainList = mStationInfo->findChild<ListView*>("trainList");
StationModel *mStationModel = new StationModel(this);
trainList->setDataModel(mStationModel);

 


 

The Qml associated with the trainList ListView widget looks like this:

 

ListView {
	id: trainList
	objectName: "trainList"
	dataModel: XmlDataModel {
		source: "models/stations.xml" 
	}

	listItemComponents: [
		ListItemComponent {
			type: "listItem"

			StandardListItem {
				titleText: ListItemData.Destination
				descriptionText: ListItemData.Traincode
				statusText: ListItemData.Duein
			}
		}
	]
}

 


The only thing that shows up in the ListView is the Destination attribute. 
Does anybody have any ideas about what I'm doing wrong?

 

Thanks,

Evan 

New Developer
Posts: 4
Registered: ‎05-21-2012
My Device: Dev Alpha
My Carrier: Meteor

Re: Linking a GroupDataModel to ListView

I'm going to reply to my own question.

 

I solved the problem. There wouldn't have been a problem in the first place if I had read the documentation more closely, but I do love to dive in as soon as possible, and the solution was not to be found in the Cascades Lists tutorial.

 

You have to implement an itemType function in javascript withing the ListView to tell it what type of list item to use for each item in the data model.

 

Everything started working when I included this:

 

// itemType mapping
function itemType(data, indexPath) {
    if (indexPath.length == 1) {
        return "header";
    }
    return "listItem";
}

 

The index path length is 1 for the headers because I used the "Destination" key in the QMap as the first sorting key..

 

this->setSortingKeys(QStringList() << "Direction" << "Duein");

 

It turns out the GroupDataModel was mapped to the ListView perfectly. The problem was in the QML. Hopefully posting the answer here will help someone else who runs into the same problem.

Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Linking a GroupDataModel to ListView

See also sample stampcollector, main.qml, declaration of the listItemComponents in ListView stampList.