Titanium将自定义模板添加到列表视图

时间:2014-06-24 14:16:41

标签: listview titanium-alloy

我目前正在使用Titanium studio和Alloy构建应用程序。 在我的一个Windows中,我试图通过按下某个按钮(允许用户检索图像或某个文件)在listView中动态添加ListItem。

我需要添加一些带有一些特定结构的listItem:一个应该显示dataType的图像,一个用于文件名的标签,另一个用于删除listItem的图像。 这是我的模板:

ligneFichier.xml

<Alloy>
<ItemTemplate name="ligneFichier" id="ligneFichier">
     <View class="item-container">               
         <ImageView bindId="typeDonnee" />
         <Label bindId="nomFichier" />  
         <ImageView bindId="supprimer" />
     </View>
</ItemTemplate>
</Alloy>

然后,在我页面的控制器中:

myController.js

var data = [];
        var tmp = {
            typeDonnee : {
                image : '/images/image.png'
            },
            nomFichier : {
                text : event.media.file.name
            },
            supprimer : {
                image : '/images/supprimer.png'                 
            }
            //I tried to use this line :
            //template: 'ligneFichier',
            //But it tells me that template is undefined
        };
        data.push(tmp);

        //My listView
        $.listeFichiers.sections[0].items = $.listeFichiers.sections[0].items.concat(data);

所以我尝试直接将模板与合金链接起来:

       <ListView id="listeFichiers" height="100" headerTitle="" template="ligneFichier">
            <ListSection id="photo" headerTitle="">

            </ListSection>
            <ListSection id="audio" headerTitle="">

            </ListSection>
        </ListView>

但是,当我添加一行时,它不使用我的模板,它甚至找不到文本,它只写'标签'。 然后,在控制台中,有一条消息:

  

请使用buildInTemplate的“属性”绑定

所以我试图用'属性'替换绑定名称,但没有成功...... 这对某人意味着什么吗?如果我忘记了一些样品,请不要犹豫,或者告诉我。

1 个答案:

答案 0 :(得分:5)

我认为合金无法识别您的模板,因为它是在单独的文件中定义的。 尝试在同一个文件中定义它:

 <ListView id="listeFichiers" height="100" headerTitle="" defaultItemTemplate='ligneFichier'>
  <Templates>
   <ItemTemplate name="ligneFichier" id="ligneFichier">
     <View class="item-container">               
        <ImageView bindId="typeDonnee" />
        <Label bindId="nomFichier" />  
        <ImageView bindId="supprimer" />
     </View>
  </ItemTemplate>
 </Templates>
   <ListSection headerTitle="Title">
     <ListItem typeDonnee:image="yourimage.png" nomFichier:text="FileName" supprimer:image="supprimer.png" />
   </ListSection>
 </ListView>

编辑:您可以使用require添加模板: