【问题标题】:Items in list is never selected列表中的项目永远不会被选中
【发布时间】:2013-12-17 14:48:17
【问题描述】:
我有一个列表,将列表设置为它的 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;
}
}
}
【问题讨论】:
标签:
apache-flex
flex4
flex-spark
【解决方案1】:
有两个问题。
我对 ArrayCollection 应用了排序,但该字段不在项目中。我从另一个项目复制了代码,该字段是“@name”,因为它是一个 XMLListCollection。排序字段应设置为“名称”。
因此,当您设置 selectedItem 属性时,它会在集合中查找,如果集合具有排序,那么它会在 findItem() 调用中查找,该调用执行比较函数来检查项目是否具有项目中的字段名称。如果不是,它会引发错误。由于我的字段名称不正确,因此引发了错误。如果抛出错误,则放弃寻找选定项目的追求,选定索引为-1。
ListCollectionView.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;
来自 Sort.as 的代码:
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);
}
}
第二个问题是 List 使用完全相等运算符,因此它使用“===”而不是“==”。这意味着您必须确保传入列表中项目的确切实例。