2013-12-17 168 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); 
     } 
    } 

的第二個問題是,列表使用的精確相等運算符所以它使用 「===」而不是「==」。這意味着您必須確保您傳遞列表中項目的確切實例。