Search code examples
actionscript-3apache-flexflex4.5arraycollection

How can be a ArrayCollection created?


There is a nice example of Advanced Datagrid and use of ArrayCollection here. I like to have something like that;

private var dpFlat:ArrayCollection = new ArrayCollection([
{Region:"Southwest", Territory:"Arizona", 
  Territory_Rep:"Barbara Jennings", Actual:38865, Estimate:40000}, 
{Region:"Southwest", Territory:"Arizona", 
  Territory_Rep:"Dana Binn", Actual:29885, Estimate:30000},  
{Region:"Southwest", Territory:"Central California", 
  Territory_Rep:"Joe Smith", Actual:29134, Estimate:30000},  
{Region:"Southwest", Territory:"Nevada", 
  Territory_Rep:"Bethany Pittman", Actual:52888, Estimate:45000},  
{Region:"Southwest", Territory:"Northern California", 
  Territory_Rep:"Lauren Ipsum", Actual:38805, Estimate:40000}, 
{Region:"Southwest", Territory:"Northern California", 
  Territory_Rep:"T.R. Smith", Actual:55498, Estimate:40000},  
{Region:"Southwest", Territory:"Southern California", 
  Territory_Rep:"Alice Treu", Actual:44985, Estimate:45000}, 
{Region:"Southwest", Territory:"Southern California", 
  Territory_Rep:"Jane Grove", Actual:44913, Estimate:45000}
]);

I need to read an XML file and get some attributes from it. Then create a custom ArrayCollection with these attributes. Right now I am creating a custom XML but I no longer want to use XML and instead check Array Collection.

Here is the code I read and create another XML;

kitapKonuSayisi = _tempXMLOR.Body.Ogrenci.length();

for(var itra:int = 0; itra < kitapKonuSayisi; itra++)
{
    uniteAdi = _tempXMLOR.Body.Ogrenci[itra].UniteAdi;
    konuAdi  = _tempXMLOR.Body.Ogrenci[itra].KonuAdi;
    ogrenciAyrintiRaporArray.push([uniteAdi, konuAdi]);
}

for(var itrc:int = 0; itrc < kitapKonuSayisi; itrc++)
{
    kitapUniteSayisi = _tempXMLOR.Body.Ogrenci[itrc].UniteAdi.length();
    uniteAdi = _tempXMLOR.Body.Ogrenci[itrc].UniteAdi;
    if(raporStringUnitCheck === true)
    {
        ogrenciAyrintiRaporStr += ('\t<Unite name="' + uniteAdi + '" yuzde="' + 100 + '">\n');
        raporStringUnitCheck = false;
    }

    for(var itrd:int = 0; itrd < kitapUniteSayisi; itrd++)
    {
        if(ogrenciAyrintiRaporArray[itrd][0] == _tempXMLOR.Body.Ogrenci[itrc].UniteAdi)
        {
            konuAdi = _tempXMLOR.Body.Ogrenci[itrc].KonuAdi;
            ogrenciAyrintiRaporStr += ('\t\t<Konu name="' + konuAdi + '" yuzde="' + 100 + '"/>\n');
        }
    }
}
if(raporStringUnitCheck === false && raporStringCompCheck === false)
{
    ogrenciAyrintiRaporStr += ('\t</Unite>\n');
    ogrenciAyrintiRaporStr += ('</Rapor>');
    raporStringCompCheck = true;
    raporString2XML = new XML(ogrenciAyrintiRaporStr);
}

So how can I create ArrayCollection and populate elements in that scheme?


Solution

  • So looks like you have an Array "ogrenciAyrintiRaporArray" already. You can create an ArrayCollection out of that:

    var myArrayCollection:ArrayCollection = new ArrayCollection(ogrenciAyrintiRaporArray);
    

    It's a bit strange that you have an Array consisting of arrays

    ogrenciAyrintiRaporArray.push([uniteAdi, konuAdi]);
    

    Usually there are objects in an ArrayCollection that you can easily use to display data in lists, datagrids etc. Also, it is easier to access properties in an object rather than indexes in an array. So you might want to change it to:

    ogrenciAyrintiRaporArray.push({uniteAdi:uniteAdi, konuAdi:konuAdi});
    

    Then you have an ArrayCollection of objects and can access data like

    var uniteAdi:* = myArrayCollection.getItemAt(0).uniteAdi;
    

    instead of

    var uniteAdi:* = myArrayCollection.getItemAt(0)[0];
    

    Which makes it unclear what is at the position 0, 1 or 2 in your data array