Listview in BB 10 follows MVC architecture. Here we will learn how to add custom rows to listview. To start, we have to look into 4 topics.
The data of the listview is present in DataModel. ListitemProvider is a factory class which creates the listitem with the data present in the DataModel. At run time, listview will pass the datamodel to the ListItemProvider which then uses DataModel and creates a ListItem. The ListItem is a single row of the ListView.
- Listview component (View)
- DataModel (Model)
- ListItemProvider (Controller)
- ListItem
The data of the listview is present in DataModel. ListitemProvider is a factory class which creates the listitem with the data present in the DataModel. At run time, listview will pass the datamodel to the ListItemProvider which then uses DataModel and creates a ListItem. The ListItem is a single row of the ListView.
Let us jump into the example for more practical view.
QuotesListItemProvider *myItemProvider = new QuotesListItemProvider(0); ArrayDataModel *listDataModel = new ArrayDataModel(); // Here Add data to the listmodel . // In my case I added strings to the dataModel. ListView *listView = pane->findChild("quotesList"); listView->setDataModel(listDataModel); listView->setListItemProvider(myItemProvider);
In the above code, I extracted the listview from the QML, initialised ArrayDataModel, appended data to the model, initialized costom ListItemProvider (QuotesListItemProvider), attached datamodel and listitemprovider to the listview.
To define a CustomListItemProvider class it is mandatory to inherit ListItemProvider class. Our custom class should override createItem() and updateItem() functions of ListItemProvider.
QuotesListItemProvider.h is
#include <bb/cascades/ListItemProvider> #include <bb/cascades/ListView> #include <bb/cascades/Container> #include <bb/cascades/VisualNode> #include <QVariant> using namespace bb::cascades; class QuotesListItemProvider: public bb::cascades::ListItemProvider { public: QuotesListItemProvider(Container *container); virtual ~QuotesListItemProvider(); VisualNode * createItem(ListView* list, const QString &type); void updateItem(ListView* list, VisualNode *listItem, const QString &type, const QVariantList &indexPath, const QVariant &data); private: Container *container; };
QuotesListItemProvider.cpp is:
#include "QuotesListItemProvider.h" #include "QuotesListItem.h" #include <bb/cascades/StandardListItem> #include <bb/cascades/LayoutProperties> QuotesListItemProvider::QuotesListItemProvider(Container *container) { this->container = container; } QuotesListItemProvider::~QuotesListItemProvider() { } VisualNode * QuotesListItemProvider::createItem(ListView* list, const QString &type) { QuotesListItem *myItem = new QuotesListItem(container); return myItem; } void QuotesListItemProvider::updateItem(ListView* list, bb::cascades::VisualNode *listItem, const QString &type, const QVariantList &indexPath, const QVariant &data) { QuotesListItem *myItem = static_cast(listItem); myItem->setTitle(data.toString()); }
In createItem() function, ListItem's are created. The created listitems are then passed to updatedItem() function. Observe the first line of UpdateItem method, VisualNode is casted to CustomListItem(QuotesListItem). And also the parameter 'data' of QVariant type in updateItem() function is the data of our ArraydataModel in ListView. As our DataModel contains strings, I'm converting this into the string format and updated label in QuotesListItem class.
For creating a list row ( Custom ListItem), we have to inherit CustomControl.
QuotesListItem.h is
#include <QObject> #include <QUrl> #include <iostream> #include <bb/cascades/CustomControl> #include <bb/cascades/QmlDocument> #include <bb/cascades/Container> #include <bb/cascades/Image> #include <bb/cascades/ImageView> #include <bb/cascades/Label> #include <bb/cascades/controls/activityindicator.h> using namespace bb::cascades; class QuotesListItem: public CustomControl { Q_OBJECT public: virtual ~QuotesListItem(); void setTitle(QString text); QuotesListItem(Container *container); private: QString pictureURL; Label *label; void init(); };
QuotesListItem.cpp is
#include "QuotesListItem.h" #include "bb/cascades/StackLayout" QuotesListItem::QuotesListItem(Container* container) : CustomControl(container) { QmlDocument *document = QmlDocument::create("asset:///QuotesListItem.qml"); Container *rootContainer = document->createRootObject(); label = rootContainer->findChild
In this class, I'm inflating a QML and used setRoot function of CustomControl class to attach the QML's root object. setTitle() function is used to set the text. This function is called from updateItem() of QuoteListItemProvider class.
Nice tutorial. You Rocking dude....
ReplyDeleteThanks ...
ReplyDelete