Tianium ListView如何根据Alloy任务的状态显示图像?

Tianium ListView如何根据Alloy任务的状态显示图像?,listview,titanium,Listview,Titanium,我将开始使用钛合金的第二周,我不知道如何根据任务的状态显示图像,例如,我希望在列表视图中显示选中的绿色图标,如果任务未完成,则显示选中的空白图标 我试过用这个函数查询数据库 function mostrarLista(){ var tasks = Alloy.createCollection('todo'); // The table name is the same as the collection_name value from the 'config.adapt

我将开始使用钛合金的第二周,我不知道如何根据任务的状态显示图像,例如,我希望在列表视图中显示选中的绿色图标,如果任务未完成,则显示选中的空白图标

我试过用这个函数查询数据库

function mostrarLista(){

    var tasks = Alloy.createCollection('todo');
        // The table name is the same as the collection_name value from the 'config.adapter' object. This may be different from the model name.
        var table = tasks.config.adapter.collection_name;
        // use a simple query
        var query = tasks.fetch({query:'select status from ' + table + ' where status=" + 1 + "'});
        //books.fetch({query: {statement: 'DELETE from ' + table + ' where title = ?', params: [args.titulo] }});


    var section = $.elementsOnList;
        var item = section;
        var currentImage = item.icon.image;

    if(query==1){
    item.icon.image = "images/checked.png" ;
        }else{
    item.icon.image = "images/blank_check.png";
    }



}
但我不知道如何在不使用事件的情况下,以友好方式将图像设置为列表视图

这是我在控制器里的东西

listdo.js

    var args = arguments[0] || {};

var myTasks=Alloy.Collections.todo;
    myTasks.fetch();



//FUNCION PARA CAMBIAR IMAGEN 
function checked(e){
        //$.alertDialog.show();

        //$.alertDialog.addEventListener('click', function(event) {
             // Get the clicked item

        var section = $.elementsOnList.sections[e.sectionIndex];
        var item = section.getItemAt(e.itemIndex);
        var currentImage = item.icon.image;


                //Antiguo
                     if(currentImage =="images/blank_check.png" && item.properties.estatus ==1){    
                        item.icon.image = "images/checked.png" ;

                        //Change the status in order to know which task has or not completed
                        updateID(item.properties.idTareas, 2);
                        alert('Tarea realizada');
                        //end update status

                     }else{
                        item.icon.image = "images/blank_check.png";
                        updateID(item.properties.idTareas, 1);
                        alert('Tarea activa');
                    }


        section.updateItemAt(e.itemIndex, item);

}

function showDetailTask(event){

    var itemSection = $.elementsOnList.sections[event.sectionIndex]; // Get the section of the clicked item
     // Get the clicked item from that section
    var item = itemSection.getItemAt(event.itemIndex);

    var args= {
        idTareas: item.properties.idTareas,
        nombre: item.properties.nombre, //Lo que captura el array 
        hora: item.properties.hora,
        fecCreacion: item.properties.fecCreacion, //Lo que captura el array 
        fechaTarea: item.properties.fechaTarea,
        descripcion: item.properties.descripcion, //Lo que captura el array 
        estatus: item.properties.estatus

    };
    var taskdetail = Alloy.createController("taskdetail", args).getView();
    taskdetail.open();

}

function updateID(idTareas, status){
    var task = myTasks.get(idTareas);
    task.set({
        "status":status  //Por defecto es 1 cuando esta activo si es cero quiere decir que está inactivo
    }).save();

}
在我的列表视图中

<Alloy>
    <Collection src="todo" /> <!--Capture the collections I want to show -->
        <Tab id="tabPrincipal" title="Listado de tareas">       
    <Window>

                <AlertDialog id="alertDialog" title="Información" message="¿Desea marcar como completada está tarea?" cancel="1">
                        <ButtonNames>
                            <ButtonName>Si</ButtonName>
                            <ButtonName>No</ButtonName>
                        </ButtonNames>          
                              <!--
                            Only on Android, an additional view can be added to be rendered in
                            the AlertDialog, replacing any declared buttons. It will be added
                            to the AlertDialog's view hierarchy like any other View.
                        -->
                </AlertDialog>

                <ListView id="elementsOnList" defaultItemTemplate="elementTemplate" > <!--onItemClick= -->
                                        <Templates>
                                                <ItemTemplate name="elementTemplate">
                                                  <View>
                                                    <Label bindId="textoLista" id="textoLista" onClick="showDetailTask"/>
                                                    <ImageView bindId="icon" id="icon" onClick="checked"/> 
                                                  </View>
                                                </ItemTemplate>
                                            </Templates>
                                       <ListSection dataCollection="todo">

                                          <ListItem 
                                                icon:image="images/blank_check.png" 

                                                textoLista:text="{name}" 
                                                idTareas="{idTarea}" 
                                                nombre="{name}" 
                                                hora="{hour}" 
                                                fecCreacion="{dateCreated}"
                                                fechaTarea="{dueDate}" 
                                                descripcion="{description}" 
                                                estatus="{status}" 
                                            />  

                                       </ListSection>             
                </ListView>
    </Window>
        </Tab>
</Alloy>

硅
不

提前感谢。

您可以使用
dataTransform
功能将imageView绑定到选中或空白图像

以下是您应该做的:

在.xml文件中:

<Alloy>
  <Collection src="todo" />
            ...
            ...
    <ListView id="elementsOnList" defaultItemTemplate="elementTemplate" >
       <ListSection dataCollection="todo" dataTransform="myDataTransformFunction">
        <ListItem
           icon:image="{image}" 
           textoLista:text="{name}" 
           idTareas="{idTarea}" 
           nombre="{name}" 
           hora="{hour}" 
           fecCreacion="{dateCreated}"
           fechaTarea="{dueDate}" 
           descripcion="{description}" 
           estatus="{status}"/>  
       </ListSection>

有关数据绑定的更多详细信息,请参阅

为什么不使用事件?因为我使用两个选项卡构建了我的应用程序,一个选项卡显示用于添加TAK的表单,另一个选项卡显示任务状态,因此,我不知道如何使用事件,因为我的想法是在按下选项卡时向用户显示ListView中的不同图像,这取决于状态。这绝对是太棒了!,我很感激你的时间代码工作得很好,我会仔细阅读你给我的文档链接。再次感谢你让我开心。
function myDataTransformFunction(model){
  var transform = model.toJSON();
  //here I add a custom field to the model.
  transform.image = (transform.status == 1) ? "images/checked.png" : "images/blank_check.png";
  return transform;
}