对于循环在Flash Builder 4.5中不一致递增

时间:2012-02-10 18:40:03

标签: flex for-loop flash-builder

以下功能旨在创建多个学生信息数组。 studentList数组显示为带有元素“All,A,B,C,D等”的组合框。 comboBox设置为在更改时调用此函数。 comboBox的默认selectedIndex为零,因此在初始显示时,它将设置为“All”,并列出所有学生。

用户在comboBox中选择“B”,只列出姓名以B开头的学生。然后用户选择“Z”并且仅列出Z学生。太棒了。

如果现在用户选择“全部”,则列表为空。再次选择“A”,我们得到A学生。选择“全部”我们什么都没得到。

我把“点击数”放在那里,以便更好地解释发生了什么。

在初始显示时,命中计数显示600,学生所有收集大小显示600,这正确地是列表中学生的总数。

当选择一个字母时,点击计数会正确显示学生现在在列表中的数字,并且学生所有尺寸都保持为600.

然而,当再次选择“全部”时,命中计数变为1并且列表为空。学生所有尺寸都保持在600。

这里有什么问题。我没有看到_index应该增加,但只有在选择“全部”时才会增加。这就好像studentsAll.Length值返回1而不是600.但我检查了它正确返回600.

“所有”一次有效,但从未再次出现过。虽然每次过滤字母时该方法仍然可以正常工作。

TIA提供任何帮助。

约翰

private function filterStudents(): void {
    studentsList.removeAll();
    studentsIDs.removeAll();
    studentsEmailpws.removeAll();
    studentspws.removeAll();

    var _studentNameFirstLetter:String;
    var _filterLetter:String = alphaFilterCombo.text;

    var _i:int=0;
    var _doAll:Boolean = alphaFilterCombo.selectedIndex == 0;
    var _doIt:Boolean = true;

        //note:studentsAll is an xmlCollection of all the students filled on startup
    for (var _index:int=0; _index < studentsAll.length; _index++) {
        if (!_doAll) {
            _studentNameFirstLetter = studentsAll[_index].name;
            _studentNameFirstLetter = _studentNameFirstLetter.substring(0,1);
            _doIt =  (_studentNameFirstLetter == _filterLetter);

        } 

        if (_doIt) {
            _i ++;
            formTitleLabel.text = "hit count = "+ _i.toString() + " studentsAll size = " + studentsAll.length.toString();
            studentsList.addItemAt(studentsAll[_index].name, studentsList.length);
            studentsIDs.addItemAt(studentsAll[_index].id, studentsIDs.length);
            studentsEmailpws.addItemAt(studentsAll[_index].emailpw, studentsEmailpws.length);   
            studentspws.addItemAt(studentsAll[_index].pw, studentspws.length);  

        }

    }   
}

附录:使用内置过滤功能的代码...

private function filterStudents(): void {
    var filterFunctionToUse : Function;
    studentsList.removeAll();

    searchedChar = alphaFilterCombo.text;
    filterFunctionToUse = studentLastNameFilter;    
    studentsAll.filterFunction = filterFunctionToUse;
    studentsAll.refresh();

    for (var _index:int=0; _index < studentsAll.length; _index++) {
        studentsList.addItemAt(studentsAll[_index].name, studentsList.length);      
    }   
}

private function studentLastNameFilter(value : Object) : Boolean{
    return  (value.name as String).charAt(0) == searchedChar;

}

附录2:以下是HTTPService的结果方法,该方法返回填充学生的数据库中的学生所有...

[Bindable] private var studentsAll:XMLListCollection = new    XMLListCollection(); 
[Bindable] private var studentsList:XMLListCollection;   
[Bindable] private var studentsIDs:XMLListCollection;    
[Bindable] private var studentsEmailpws:XMLListCollection;
[Bindable] private var studentspws:XMLListCollection;

private function lookupStudentsSuccess(event:ResultEvent) : void { 
    var _studentsXML:XML = XML(event.result);   
    studentsAll = new XMLListCollection(_studentsXML.details);      
    studentsList = new XMLListCollection;
    studentsIDs = new XMLListCollection;
    studentsEmailpws = new XMLListCollection;
    studentspws = new XMLListCollection;

    for (var _index:int=0; _index < studentsAll.length; _index++) {
        studentsList.addItemAt(studentsAll[_index].name, studentsList.length);
        studentsIDs.addItemAt(studentsAll[_index].id, studentsIDs.length);
        studentsEmailpws.addItemAt(studentsAll[_index].emailpw, studentsEmailpws.length);   
        studentspws.addItemAt(studentsAll[_index].pw, studentspws.length);  
    }   
}

1 个答案:

答案 0 :(得分:0)

我改变了一下它是如何工作的,我认为这将是一个更清晰的解决方案,使用内置的filterFunction属性。

import mx.collections.ArrayCollection;
import mx.collections.XMLListCollection;

private var searchedChar;
private var studentsList : ArrayCollection;
private var studentsIDs : ArrayCollection;
private var studentsEmailpws : ArrayCollection;
private var studentspws : ArrayCollection;
private var studentsAll : XMLListCollection;

private function filterStudents() : void
{
    var filterFunctionToUse : Function;
    var _doAll : Boolean = alphaFilterCombo.selectedIndex == 0;
    if (_doAll)
        filterFunctionToUse = null;
    else
    {
        searchedChar = alphaFilterCombo.text;
        filterFunctionToUse = aFilterFunction;
    }
    studentsAll.filterFunction = filterFunctionToUse;
    studentsAll.refresh();

    //note:studentsAll is an xmlCollection of all the students filled on startup

    for (var _index : int = 0; _index < studentsAll.length; _index++)
    {
        formTitleLabel.text = "hit count = " + (_index + 1).toString() + " studentsAll size = " + studentsAll.length.toString();
        studentsList.addItemAt(studentsAll[_index].name, studentsList.length);
        studentsIDs.addItemAt(studentsAll[_index].id, studentsIDs.length);
        studentsEmailpws.addItemAt(studentsAll[_index].emailpw, studentsEmailpws.length);
        studentspws.addItemAt(studentsAll[_index].pw, studentspws.length);

    }
}

private function aFilterFunction(value : Object) : void
{
    return (value.name as String).charAt(0) == searchedChar;

}