2012-08-21 2 views
0
public var streamList:ArrayCollection=new ArrayCollection(); 
    //am getting the streamList dynamically like WebUser7,WebUser11.....  
    private function streamSynch(event:SyncEvent):void 
    { 
     if(streamList.length>0){ 
     //(streamList)here in streamList am getting the all values(old) 
      streamList.removeAll(); 
     } 
     var results:Object = event.target.data; 
     for(var item:String in results) {    
      streamArray.push(item);  
     } 
     streams = new ArrayCollection(streamArray); 
     streamList=streams; //(streamList)here in streamList am getting the all values(new) like Webuser9,WebUser2,WebUser11...... 
     From my Example i need to add the Webuser9,WebUser2,WebUser11,WebUser7  
    } 

나는ActionScript3의 ArrayCollection에서 이전 값을 대체하여 새 값을 추가하는 방법은 무엇입니까?

답변

1

당신은 ArrayCollection 방법 setItemAt(item:Object, index:int):Object를 사용하여이 작업을 수행 할 수 있습니다 ... (가) 과거와 비교 한 다음 이전을 교체하고 새를 추가해야합니다. 대체하려는 요소의 색인 만 알면됩니다.

편집 : 사용 예제는

있습니다

function arrayCollectionExample() : void { 

    // create instance of array collection 
    var arrayCollection : ArrayCollection = new ArrayCollection(); 

    // create some object that we will add to collection 
    var obj1 : Object = { name : "Object1" }; 
    var obj2 : Object = { name : "Object2" }; 
    var obj3 : Object = { name : "Object3" }; 

    // add those objects to collection 
    arrayCollection.addItem(obj1); 
    arrayCollection.addItem(obj2); 
    arrayCollection.addItem(obj3); 

    // and now let's create a new object that we will want add instead of obj2 
    var obj4 : Object = { name : "Object4" }; 

    // lets say that we don't remember that index of obj2 in a collection is "1". We have to get from collection 
    var obj2Index : int = arrayCollection.getItemIndex(obj2); 

    trace(obj2Index) // 1 

    // now when we know index we can replace obj2 with obj4 
    arrayCollection.setItemAt(obj4, obj2Index); 

    // now when we have replaced values we can see that item at index "1" property "name" is "Object4" 
    trace(arrayCollection.getItemAt(obj2Index)["name"]) // "Object4" 

} 
관련 문제