2013-12-17 61 views
1

我有一个列表并将列表设置为dataProvider中的确切项目不会以编程方式进行选择。这里是代码:从未选择列表中的项目

  if (list.selectedItem != iDocument) { 

       var length:int = documentsCollection.length; 
       for (var i:int;i<length;i++) { 
        jDocument = IDocumentData(documentsCollection.getItemAt(i)); 


        if (jDocument.uid==iDocument.uid) { 
         list.selectedItem = IDocumentData(documentsCollection.getItemAt(i)); 
         break; 
        } 
       } 
      } 

回答

0

有两个问题。

我已经对ArrayCollection应用了一种排序,并且该字段不在该项目中。我从另一个项目复制了代码,并且该字段是“@name”,因为它是一个XMLListCollection。排序字段应该已设置为“名称”。

因此,当您设置selectedItem属性时,它会在集合中查找,并且如果集合具有排序,那么它会在findItem()调用中执行比较函数,该函数检查项目是否具有项目中的字段名称。如果不是,则会引发错误。由于我的字段名称不正确,所以引发了错误。如果抛出错误,则放弃寻找所选项目的追求,并且选择索引为-1。从ListCollectionView.as

代码:从Sort.as

try 
    { 
     return sort.findItem(localIndex, values, mode, insertIndex); 
    } 
    catch (e:SortError) 
    { 
     // usually because the find critieria is not compatible with the sort. 
    } 

    return -1; 

代码:

var hasFieldName:Boolean; 
    try 
    { 
     hasFieldName = values[fieldName] !== undefined; 
    } 
    catch(e:Error) 
    { 
     hasFieldName = false; 
    } 
    if (hasFieldName) 
    { 
     if (!hadPreviousFieldName) 
     { 
      message = resourceManager.getString(
       "collections", "findCondition", [ fieldName ]); 
      throw new SortError(message); 
     } 
     else 
     { 
      fieldsForCompare.push(fieldName); 
     } 
    } 

的第二个问题是,列表使用的精确相等运算符所以它使用 “===”而不是“==”。这意味着您必须确保您传递列表中项目的确切实例。