如何在Flex中将String转换为ArrayCollection?

时间:2012-12-17 13:07:51

标签: flex flex4

实际上是我的Flex应用程序..示例代码

private var selectedDays:String = null;
            protected function selectRepeatedDays(event:MouseEvent):void
            {
                selectedDays = new String();
                if(MON.selected==true)
                {
                    selectedDays += "MONDAY,";
                    Alert.show("Monday  :"+selectedDays);
                }
                if(TUE.selected==true)
                {
                    selectedDays +=  "TUESDAY,";
                }
                if(WED.selected==true)
                {
                    selectedDays += "WEDNESDAY,";
                    Alert.show("Monday  :"+selectedDays);
                }
                if(THU.selected==true)
                {
                    selectedDays += "THURSDAY,";
                }

}
var arr:ArrayCollection = new ArrayCollection();
arr = selectedDays.substr(0, selectedDays.length-1).toString();

Alert.show(arr.lenth)

但它没有转换......警告声明不提示.. 那么如何将此字符串转换为ArrayCollection ...

2 个答案:

答案 0 :(得分:2)

使用方法split将String转换为Array:

var array:Array = selectedDays.split(",");

然后(如果需要)将Array的每个项添加到ArrayCollection:

var arr:ArrayCollection = new ArrayCollection();
for each (var str:String in array) {
    arr.addItem(str);
}

答案 1 :(得分:1)

使用下面的代码段将String转换为ArrayCollection

  1. 使用拆分方法

    将字符串转换为数组
    var array:Array = selectedDays.split(",");
    
  2. 将数组转换为ArrayCollection

    var selectedDaysArr:ArrayCollection = new ArrayCollection(array);