Bojan Kogoj
Bojan Kogoj

Reputation: 5649

ListView isn't using custom ListItemComponent

I'm trying to use a custom ListItemComponent in my cascades however it is ignoring it. Instead of drawing my Label, coloring it cyan and putting ListItemData.text in, it fills my list with ListItemData.description (my json has text, description, status and image). You can see screenshot here. If i use StandardListItem all information is displayed properly.

QML:

Page {
    Container {
        ListView {
            id: myListView
            dataModel: MyListModel {
                id: myListModel
            }
            listItemComponents: [
                ListItemComponent {
                    type: "listItem"

                    Container {
                        id:item
                        layout: StackLayout {
                            orientation: LayoutOrientation.LeftToRight
                        }
                        Label {
                            id: text
                            text: ListItemData.text
                            textStyle {
                                color: Color.Cyan
                            }
                        }
                    }
                }
            ]
        }
    }
    onCreationCompleted: {
        myListModel.load("app/native/assets/mydata.json")
    }
}

c++:

void MyListModel::load(const QString& file_name)
{
    bb::data::JsonDataAccess jda;
    QVariantList lst = jda.load(file_name).value<QVariantList>();
    if (jda.hasError()) {
        bb::data::DataAccessError error = jda.error();
        qDebug() << file_name << "JSON loading error: " << error.errorType() << ": " << error.errorMessage();
    }
    else {
        qDebug() << file_name << "JSON data loaded OK!";
        append(lst);
    }
}

QVariant MyListModel::value(int ix, const QString &fld_name)
{
    QVariant ret;
    if(ix >= 0 && ix < size()) {
        QVariantMap curr_val = QVariantListDataModel::value(ix).toMap();
        ret = curr_val.value(fld_name);
    }
    return ret;
}

void MyListModel::setValue(int ix, const QString& fld_name, const QVariant& val)
{      
    if(ix >= 0 && ix < size()) { 
        QVariantMap curr_val = QVariantListDataModel::value(ix).value<QVariantMap>();
        curr_val[fld_name] = val;
        replace(ix, curr_val);
    }
}

Upvotes: 2

Views: 993

Answers (2)

Saravana Kumar
Saravana Kumar

Reputation: 1

In the ListItemComponent{ }, you have a "type:" field, you should set the value as "item", ie. type:"item". Then the list view will use custom component

Upvotes: 0

nonesuchnick
nonesuchnick

Reputation: 627

This depends on the type your DataModel implements/inherits from. When a ListView needs to know the type of an item for display, in most cases (see ListItemTypeMapper for the exception) it calls itemType() from the DataModel. By default most datamodels will return an empty string, but a GroupDataModel will return either "header" or "item" depending on whether the item is a header or not.

You have specified that your ListItemComponent is only valid for items of type "listItem", which most probably does not match what your MyListModel::itemType() is returning. If MyListModel is a GroupDataModel, then change your ListItemComponent to be for type: "item", otherwise use type: "" or remove the type property altogether so that your ListItemComponent is used by default for all items.

Upvotes: 4

Related Questions