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
Trusted Contributor
Brennan12325
Posts: 204
Registered: ‎05-15-2012
My Device: None
Accepted Solution

ListView no header

Hey, I'm looking for a way to use a ListView that's only one level deep (ie, has no headers). I'm trying to make the stamp collector example work in such a fashion but I'm having a lot of issues pulling it off.

 

Using this code I get all the stamps under one blank header:

    mStampModel = new GroupDataModel();
    mStampModel->setParent(this);
    mStampModel->clear();
    mStampModel->insert(mainList);

 

but the blank header still shows up.

 

I suspect GroupDataModel is the wrong data model for this but QListDataModel also seems to want to create a header and I see DataSetModel is marked as to be deprecated (not sure if it would accomplish what I want anyways).

 

Thoughts, ideas, solutions?

----------------------
Check out my app, Alien Flow for reddit

And of course, like my post if you found it helpful or informative!
Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 668
Registered: ‎11-29-2011
My Device: developer

Re: ListView no header

I have a feeling that it's the ListView that is responsible for your visualization, and not necessarily the data model.

 

https://bdsc.webapps.blackberry.com/cascades/reference/bb__cascades__ListView.html

 

There are references to changing list visuals there and references to using HeaderListItem vs StandardListItem.  Certainly a Cascades guru can jump in at the appropriate time and set the record straight :smileyhappy:

 

Cheers,

Sean

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer

Re: ListView no header

The GroupDataModel is not what you want.  It infers the headers from the data; if you iterate through its data in stamp collector you'll find it iterates through the raw data, not the displayed data.

 

I'm not sure what you mean by "one level deep" vs. "no headers" -- do you mean it displays the children flat?  I suspect what you want is your own DataModel subclass that presents the data as a single list, and set the type to be item instead of header.   I haven't tried this yet, but I suspect it does what you want.

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer

Re: ListView no header

You may find QStringListDataModel or QVariantListDataModel is sufficient for your needs.

 

In stampcollector:

- change main.qml to remove the header component.

- Change stampcollector.h to #include <bb/cascades/QListDataModel> and change mStampModel to be of type QStringListDataModel*.   (in a real app you might want to use your own typedef so you don't need to include in the include file)

- change setUpStampListModel to

    mStampModel = new QStringListDataModel;
    mStampModel->setParent(this);
    mStampModel->clear();

    mStampModel->append("hello");
    mStampModel->append("World");

 

Of course the app will only run as far as the first list, but try this out and see if it's what you need

Please use plain text.
Developer
Jeff_Lu
Posts: 384
Registered: ‎08-12-2008
My Device: 8700

Re: ListView no header

[ Edited ]

Please check  suggestion,

 

what i am doing like:

 

	bb::cascades::QVariantListDataModel *model = new QVariantListDataModel();
	QVariantMap map;
	map["subject"] = subject1"; map["body"] = "Emergency"; model->append(map);
	map["subject"] = "FYI"; map["body"] = "Patient is sleeping"; model->append(map);
	map["subject"] = "Warning..."; map["body"] = "Blood pressure is high"; model->append(map);
	list->setDataModel(model);

 or you can create your own QObject which is QVariant type.

 

However, when I create my own QVariant Object, never got it pass compile, I am working on that right now.

Please use plain text.
Trusted Contributor
Brennan12325
Posts: 204
Registered: ‎05-15-2012
My Device: None

Re: ListView no header

Hey, thanks, I ended up with this code:

 

bb::cascades::QVariantListDataModel *model = new QVariantListDataModel();
    model->setParent(this);
    model->clear();
    model->append("hello");
    model->append("world");
    model->append("world");model->append("world");model->append("world");model->append("world");model->append("world");model->append("world");model->append("world");model->append("world");model->append("world");model->append("world");
    //model->insert(0, mainList);

    StampList->setDataModel(model);

 

That does display a list with no headings.

 

When I try to add the stamp list (by commenting in the commented out code) I get the error:

//Unsupported QVariant type passed into QListDataModel::insert(i, QVariantList)!

 

Apparently the insert can't accept a QVariant of type QVariantList, I guess. Not sure how I would reimplement stamp collector with no headers with this type of model? Ideas? Basically at the end of the day I want this to be able to dynamically load JSON and convert that to a decent looking list with no headings, similar to stamp collector. The main difference being that uses a static JSON file.

 

Just out of curiousity, I've noticed that the list seems to be pretty lack luster. I'm not sure if this is just the emulator but it doesn't bounce at the ends (it doesn't even have any sort of built in scrolling deceleration, it just stops when you lift your finger). Are these things that are set in the qml document, or is this expected behavior?

 

----------------------
Check out my app, Alien Flow for reddit

And of course, like my post if you found it helpful or informative!
Please use plain text.
Trusted Contributor
Brennan12325
Posts: 204
Registered: ‎05-15-2012
My Device: None

Re: ListView no header

The code that is not behaving as I would like/expect is the insert for the type QVariantListDataModel:

 

void insert(int i, const QVariantList &values)
{
    if (i >= 0 && i <= mItemList.size()) {
        int valueCount = values.size();
        if (values[0].canConvert<Item>()) {
            for (int j = valueCount; j >= 0; --j) {
                insert(i, values[j]);
            }
        }
        else {
            qWarning() << "Unsupported QVariant type passed into QListDataModel::insert(i, QVariantList)!";
        }
    }
}

 

For some reason the JSON data, which we converted to a QVariantList of QVariantList's (as far as I can discern from what the line:

QVariantList mainList = jda.load().value<QVariantList>();

 does can't be converted to an "Item". I can't find the Item class and have no idea what's required to make a QVariantList convertible to an Item.

----------------------
Check out my app, Alien Flow for reddit

And of course, like my post if you found it helpful or informative!
Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer

Re: ListView no header

I just compared Stamp Collector on the device vs. the simulator.  The list view is significantly more elegant on the device than the simulator.

 

Stuart

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer

Re: ListView no header

Here's some interim observations, in case this unblocks you.  I hope to have a more complete answer soon.

 

As you surmise, the trick is lining up the QVariant with what the object is expecting.  I'm trying:

 

    mStampModel = new QVariantListDataModel;
    mStampModel->setParent(this);
    mStampModel->clear();

    qDebug() << mainList;

    foreach(const QVariant& variant, mainList)
    {
        qDebug() << variant;
        mStampModel->append(variant);
    }

 

The JSON data loads into QVariants that exactly mirror the json file:  a list of maps of key/value pairs.   We want a list with as many items as there are maps, not a list of one item.  I expect the people who wrote QListDataModel protected against misunderstanding by forcing people to add items individually.

 

The Item in canConvert<Item> is referring to the templated type name in template<class Item>
class QListDataModel.  (Open include file bb/cascades/QListDataModel and open its include).   QVariantListDataModel is a typedef, so Item in this case is QVariant.

 

Stuart

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer

Re: ListView no header

You also need in main.qml after the listItemComponents:

 

function itemType(data, indexPath) {
      return "item"
}

 

because in this case you want the StampItem handling.

 

Stuart

Please use plain text.