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
Enaud
Posts: 72
Registered: ‎02-08-2013
My Device: z10
My Carrier: rogers
Accepted Solution

Using a Single XML Tag for ListView

Hello All,

 

I'm trying populate my ListView with a series of specific tags from my XML file. For example, using this XML file:

<employees>
    <employee>
        <firstname>Mike</firstname>
        <lastname>Chepesky</lastname>
        <number>62145936</number>
    </employee>
    <employee>
        <firstname>Westlee</firstname>
        <lastname>Barichak</lastname>
        <number>41236500</number>
    </employee>
</employees>

 


I want to populate my ListView with only the firstnames of the employess. I load the xml file into my list view with this snippet of code:
    xmlDataModel = new XmlDataModel();
    QUrl url("file://"+QDir::currentPath()+"/data/contacts.xml");
    xmlDataModel->setSource(url);
    listView = new ListView();
    listView->setDataModel(xmlDataModel);

 


How do I ensure that my data model consists only of the tags I want to show, in this case <firstname>
 
Thanks!

Eric
Please use plain text.
Developer
Curahee
Posts: 224
Registered: ‎01-12-2013
My Device: z30
My Carrier: Mobile Vikings

Re: Using a Single XML Tag for ListView

You should do it with the ListItemComponents

 

https://developer.blackberry.com/cascades/documentation/ui/lists/list_view.html

 

I never done it in C++ but I'm sure it's also possible. Could not find the documentation for it, but it will be there somewhere.

______________________________________________________
beRail: BlackBerry World - GitHub
Mobile Vikings: BlackBerry World - GitHub
Park in Brussels: BlackBerry World
Please use plain text.
Developer
Enaud
Posts: 72
Registered: ‎02-08-2013
My Device: z10
My Carrier: rogers

Re: Using a Single XML Tag for ListView

For what it's worth this is how I resolved this:

1. Add a ListItemProvider based class to your project:

class ViewerListItemManager: public bb::cascades::ListItemProvider
{
public:
	ViewerListItemManager();

    VisualNode * createItem(ListView* list, const QString &type);

    void updateItem(ListView* list, bb::cascades::VisualNode *listItem,
            const QString &type, const QVariantList &indexPath, const QVariant &data);
};

 

2. Implement the new class:

ViewerListItemManager::ViewerListItemManager()
{
}

VisualNode * ViewerListItemManager::createItem(ListView* list, const QString &type)
{
	Q_UNUSED(list);

	if (type.compare(GroupDataModel::Header) == 0) {
		return new Header();
	} else {
		return new StandardListItem();
	}
}

void ViewerListItemManager::updateItem(ListView* list, bb::cascades::VisualNode *listItem,
        const QString &type, const QVariantList &indexPath, const QVariant &data)
{
	Q_UNUSED(list);
	Q_UNUSED(indexPath);

	if (type.compare(GroupDataModel::Header) == 0) {
		QString caption = data.value<QString>();
		static_cast<Header*>(listItem)->setTitle(caption);
	} else {
		QVariantMap map = data.value<QVariantMap>();
		static_cast<StandardListItem*>(listItem)->setTitle(QString("%1").arg(map["firstname"].toString()));
	}
}

 3. Assign the ListItemProvider to your ListView:

groupDatamodel = new GroupDataModel();
XmlDataAccess xda;
QVariant list = xda.load(QDir::currentPath()+"/data/employees.xml", "/root/employee");

groupDatamodel->insertList(list.value<QVariantList>());
listView = new ListView();
ViewerListItemManager *viewerItemProvider = new ViewerListItemManager();
listView->setListItemProvider(viewerItemProvider);
listView->setDataModel(groupDatamodel);

 

Please use plain text.
New Developer
Watman
Posts: 15
Registered: ‎02-12-2013
My Device: Bold 9900 - Z10 LE - DAC - Playbook

Re: Using a Single XML Tag for ListView

Excellent,

 

thx for solving a mutual problem :smileyhappy:

---------------------------------------------------------------------------------------------
Prompter: BlackBerry World - Built for BlackBerry™
Prompter4Q: BlackBerry World - Built for BlackBerry™
Please use plain text.