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
unbreakable
Posts: 74
Registered: ‎12-17-2009
My Device: Z30
Accepted Solution

Re-using Custom List Decorator

Hi,

 

I have created a custom to change the itemType of my lists. I would like to do something similar to the List Decorator Sample (except with different queres) but I was wondering if I can reuse the custom itemType that I have created. The example uses the sql database itself to check for "is_group" (to activate the item type) but I dont really want to cluter up my database that way (OCD)...

 

QString GroupArtistDataModelDecorator::itemType(const QVariantList& indexPath) {
        QVariant data = bb::cascades::datamanager::DataModelDecorator::data(
                indexPath);
        if (data.isValid()) {
            QVariantMap map = data.value<QVariantMap>();
            int isGroup = map["is_group"].toInt();
            // Verifies that this data item is of group and returns the new type.
            if (isGroup) {
                return QString("group");
            }
        }
        return QString("");
    }


Is there a way around it such that I can maybe use map[" "] to filter the data and, using a switch statement, return an itemType based on that?

What would be the most efficient way to do it? 

Is it also possible to add in a parameter e.g. 

QString GroupArtistDataModelDecorator::itemType(const QVariantList& indexPath, string itemTypeParameter)

And then to call it in QML:

 

 

*I hope I am not being too imaginative here. 

 

Or is there no alternative but to create individual custom datamodels for the different model types? 

 

Thanks in advance!

BlackBerry Development Advisor
sabdelsayed
Posts: 249
Registered: ‎08-17-2012
My Device: BlackBerry Z10 and Q10

Re: Re-using Custom List Decorator

[ Edited ]

Hi unbreakable, I want to assist you with this question. But before I can do that, I need to clarify with you on what exactly you are trying to achieve. Can you please explain your use case in more detail possibly sharing how your data looks like and how you are looking to do the filtering?

 

I can answer the second part of your question right away though.

 

In the listdecorator sample app, the GroupArtistDataModelDecorator is a custom class inheriting from the DataModelDecorator. Thus, you can definitely define your own itemType function and add a parameter as you are indicating in your post. And, if you want to be able to call it from QML, then you should simply add the Q_INVOKABLE macro infront of the function definition similar to how it is defined in the DataModelDecorator API:

 

virtual Q_INVOKABLE QString itemType  (const QVariantList &indexPath)

 

Also, for the detailed explanation of the 10.2 newly added listdecorators sample app, checkout this link:

http://blackberry.github.io/Cascades-Samples/listdecorators.html

@SamarAbdelsayed

Did this answer your question? Please accept post as solution.
Please refrain from posting new questions in solved threads.
Found a bug? Report it using the Issue Tracker
BlackBerry Development Advisor
sabdelsayed
Posts: 249
Registered: ‎08-17-2012
My Device: BlackBerry Z10 and Q10

Re: Re-using Custom List Decorator

[ Edited ]

I don't see any reason why you cannot use a switch in the itemType method instead of adding a new field to your DB. Below is a possible scenario for the implementation.

 

In the header file GroupArtistDataModelDecorator.hpp define the struct SubCategory:

    struct SugCategory
    {
        enum Type {
			  News = 0,
			  Technology = 1,
			  Sports = 2,
			  ...
        };
    };

 

And in the itemType method you can use a switch that switches cases based on the map["category"].toString() for e.g. 

QString GroupArtistDataModelDecorator::itemType(const QVariantList& indexPath) {
     QVariant data = bb::cascades::datamanager::DataModelDecorator::data(
                indexPath);      SubCategory::Type category_case;
     if (data.isValid()) {
         QVariantMap map = data.value<QVariantMap>();
         QString category = map["category"].toString();
        
         if(category=="News") category_case = SubCategory::News;          else if(category=="Technology") category_case = SubCategory::Technology;          else if(category=="Sports") category_case = SubCategory::Sports;          ....       switch(category_case)      {       case SugCategory::News:       return QString("News");       break;      case SugCategory::Technology:       return QString("Technology");       break;       case SugCategory::Sports:       return QString("Sports");       break;       .....       default:       break; }

 

@SamarAbdelsayed

Did this answer your question? Please accept post as solution.
Please refrain from posting new questions in solved threads.
Found a bug? Report it using the Issue Tracker
Developer
unbreakable
Posts: 74
Registered: ‎12-17-2009
My Device: Z30

Re: Re-using Custom List Decorator

Thanks Sabdelsayed.

 

That helps a lot. Yes I was trying to achieve something like that. I would assume that in:

 

QString category = map["category"].toString();

 "Category" refers to the Table's Column title?

BlackBerry Development Advisor
sabdelsayed
Posts: 249
Registered: ‎08-17-2012
My Device: BlackBerry Z10 and Q10

Re: Re-using Custom List Decorator

[ Edited ]

Glad to hear it helped. And that's correct, I'm assuming here that "category" is a field title in the database.

@SamarAbdelsayed

Did this answer your question? Please accept post as solution.
Please refrain from posting new questions in solved threads.
Found a bug? Report it using the Issue Tracker