【问题标题】:Listview with the given xml model具有给定 xml 模型的列表视图
【发布时间】:2013-08-07 11:19:58
【问题描述】:

您好,我是 Blackberry 10 级联开发的新手。

我想创建一个包含以下数据模型的列表(放置在assets文件夹中)。

Categories.xml

 <?xml version="1.0" encoding="utf-8"?>
 <MasterData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <CategoryList>
    <Category>
       <CategoryId>12</CategoryId>
       <CategoryNameEn>Banks &amp; Investments</CategoryNameEn>
       <CategoryImageName>banks.png</CategoryImageName>
       <DisplayOrder>1</DisplayOrder>
    </Category>
    <Category>
       <CategoryId>15</CategoryId>
       <CategoryNameEn>Car Rental</CategoryNameEn>
       <CategoryImageName>cars.png</CategoryImageName>
       <DisplayOrder>2</DisplayOrder>
    </Category>
    <Category>
       <CategoryId>19</CategoryId>
       <CategoryNameEn>Services</CategoryNameEn>
       <CategoryImageName>services.png</CategoryImageName>
       <DisplayOrder>3</DisplayOrder>
    </Category>
    <Category>
       <CategoryId>18</CategoryId>
       <CategoryNameEn>Real Estate &amp; Constructions</CategoryNameEn>
       <CategoryImageName>construction.png</CategoryImageName>
       <DisplayOrder>5</DisplayOrder>
    </Category>
    <Category>
       <CategoryId>2</CategoryId>
       <CategoryNameEn>Hotels &amp; Apartments</CategoryNameEn>
       <CategoryImageName>hotels.png</CategoryImageName>
      <DisplayOrder>7</DisplayOrder>
    </Category>
 </CategoryList>

我想将唯一的 CategoryNameEn 显示为列表项。

ma​​in.qml 我已经给出了这样的。

    // Create a ListView that uses an XML data model
ListView {
    dataModel: XmlDataModel {
        source: "asset:///categories.xml"
    }
    // The ListItemComponent defines how "listItem" items should appear. 
    listItemComponents: [
        ListItemComponent {
            type: "Category" //setting the node name
            Container {
                preferredWidth: 748
                preferredHeight: 50
                background: Color.Blue

                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }

                Label {
                    text: ListItemData.CategoryNameEn //setting the node 
                    verticalAlignment: VerticalAlignment.Center
                    // Apply a text style to create a title-sized font
                    // with normal weight
                    textStyle {
                        base: SystemDefaults.TextStyles.TitleText
                        fontWeight: FontWeight.Normal
                    }
                }
                Container {
                    horizontalAlignment: HorizontalAlignment.Fill
                    verticalAlignment: VerticalAlignment.Center
                    preferredWidth: 50
                    preferredHeight: 50
                    //background: Color.Blue

                    layout: StackLayout {
                        orientation: LayoutOrientation.RightToLeft
                    }
                    // Arrow image
                    ImageView {
                        verticalAlignment: VerticalAlignment.Center
                        translationX: 0
                        translationY: 0
                        imageSource: "asset:///images/arrow.png"
                        rightMargin: 10
                    }
                } // end of inner Container
            }//end of outer container
         } // end of ListItemComponent
    ]//end of listItemComponents
}//end of ListView

输出应如下图所示。

但是列表是空的。 CategoryNameEn 未绑定到列表。 我不知道我的代码有什么问题。请解决我的问题。

【问题讨论】:

  • 嗨,Kernald,你能帮我找出我的代码有什么问题吗?您能否使用上面的 xml 和 listview 并创建简单的项目来找出错误。谢谢
  • 我回答了你的第一个问题。
  • 我建议你看看控制台。您立即收到错误...

标签: listview blackberry-10 blackberry-cascades datamodel


【解决方案1】:

在这里,我没有修复您的 XML,只是一个如何修复它的小示例。 XML:

 <model>
    <item CategoryNameEn="Banks &amp; Investments" CategoryId="12"  CategoryImageName="banks.png" DisplayOrder="1"/>
    <item CategoryNameEn="Banks &amp; Investments2" CategoryId="33"  CategoryImageName="banks2.png" DisplayOrder="2" />    
 </model>

QML:

ListView {
    dataModel: XmlDataModel {

        source: "asset:///categories.xml"
    }
    // The ListItemComponent defines how "listItem" items should appear.
    listItemComponents: [
        ListItemComponent {
            type: "item" //setting the node name
            Container {
                preferredWidth: 748
                preferredHeight: 50
                background: Color.Blue

                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }

                Label {
                    text: ListItemData.CategoryNameEn //setting the node
                    verticalAlignment: VerticalAlignment.Center
                    // Apply a text style to create a title-sized font
                    // with normal weight
                    textStyle {
                        base: SystemDefaults.TextStyles.TitleText
                        fontWeight: FontWeight.Normal
                    }
                }
                Container {
                    horizontalAlignment: HorizontalAlignment.Fill
                    verticalAlignment: VerticalAlignment.Center
                    preferredWidth: 50
                    preferredHeight: 50
                    //background: Color.Blue

                    layout: StackLayout {
                        orientation: LayoutOrientation.RightToLeft
                    }
                    // Arrow image
                    ImageView {
                        verticalAlignment: VerticalAlignment.Center
                        translationX: 0
                        translationY: 0
                        imageSource: "asset:///images/arrow.png"
                        rightMargin: 10
                    }
                } // end of inner Container
            } //end of outer container
        } // end of ListItemComponent
    ] //end of listItemComponents
}    //end of ListView

【讨论】:

  • 我已经改变了上面的模型(categories.xml)。但现在我也没有得到输出。
  • 我在控制台上收到此消息。 “8 月 8 日 05:28:13.331 com.example.STCDirectory.testDev_TCDirectorye0359adc.253837444 默认 9000 注意 XmlDataModel:无法将 'asset:///models/categories.xml' 设置为源,目前仅支持文件路径。”
  • 我已经从路径中删除了asset:///,然后显示项目,但显示的是 CategoryIds 而不是 categoryNameEn。
  • 它显示文本:ListItemData.CategoryNameEn,因此应该显示。你一定对你的 XML 做了什么,检查它的命名是否正确
猜你喜欢
  • 2012-07-25
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
相关资源
最近更新 更多