We help IT Professionals succeed at work.

arraycollection with dataproviders

mokatell
mokatell asked
on
dear experts ,
i have been facing this problem since a period of time .
i have a list with ID="abc"  and dataprovider="{arrayCollection}" .... the problem is when i have an arrayCollection with one record , the list remains empty unless the arrayCollection items are 2 and above ... then the list gets the items .. i have the same problem with datagrid ... as i said only when i retrieving one record !!! any suggested solution ..
Comment
Watch Question

Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
Just do abc.validate(); once you assign.

To do that change your code to ID="abc"  click="somefunction()";

inside that do abc.dataprovider=arrayCollection; and then do abc.validate();

Author

Commented:
didnt work !
Siva Prasanna KumarPrincipal Solutions Architect
Top Expert 2006

Commented:
can u show the code?

Author

Commented:
as simple as it is :

<s:HTTPService id="getVersions" url="{commonMethods.URLlink+'getVersions.php'}" method="POST" showBusyCursor="true" result="getVersions_resultHandler(event)" />


protected function getVersions_resultHandler(event:ResultEvent):void
{
versionsArray = getVersions.lastResult.tasks.first as ArrayCollection ;
OR
abc.dataprovider = getVersions.lastResult.tasks.first as ArrayCollection

BOTH DIDN'T WORK
                  
}


<s:List id="abc" dataProvider="{versionsArray}"/>
Top Expert 2011

Commented:
Problem can be on the server side if you declared for example result as single object AMF encoder will convert to Object if you will pass List of object it will convert to ArrayCollection

Author

Commented:
<?php
session_start();
header ("Cache-control:private");
include "phpConnect.php";

$qry = "select * from checkver";
$res = mysql_query ($qry) or die (mysql_error());

echo "<tasks>" ;
while ( $info=mysql_fetch_array($res))
{
echo "<first> <ptitle>$info[0]</ptitle> <ptype>$info[1]</ptype></first>";
}
echo "</tasks>";
?>

// this is working perfectly if i have more than one record , but when it comes to a single record , the problem happens !


Top Expert 2011

Commented:
So, looks like you are creating XML but here is a problem if XML node has one children ActionScript will case to XML (XMLNode) if more than one to XMLList.

Author

Commented:
so how can i resolve this issue ?
Top Expert 2011

Commented:
Do you want to use XML?
Could It be possible that in the case of a single result you are not recieving an ArrayCollection.
If you use

ArrayCollection(getVersions.lastResult.tasks.first)

Open in new window



instead of

getVersions.lastResult.tasks.first as ArrayCollection

Open in new window


the flashplayer will fire an error. Because if you cast with "as" and the types don't match the value null is simply set. If you cast using the Constructor-like notation Cast exceptions are fired. That's why I prefer this approach much more than casting with "as".

As a side-note ... the "first" at the end of that statement would make me assume you are taking the first element of something ... sure this is the case? But if you change the cast's the problem should be easy to track down.
Top Expert 2011

Commented:
mokatell,
if you are getting XML why you are using ArrayCollection instead XMLListCollection?


http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/XMLListCollection.html

Author

Commented:
@dgofman .. i can unserstand what you are saying ..
but i was wondering if i have an arraycollection with one item it works

like this
<dataprovider>
<arraycollection>
<object> hello flex </object>
</arraycollection>
</dataprovider>

but when it comes from the webservice with one records it fires an error !
and if xmllistcollection is the solution , then where to use the arraycollection ?



Did you try what I suggested?
Top Expert 2011

Commented:
@mokatell:

if xmllistcollection is the solution , then where to use the arraycollection ?


Yes for XML data you must use XMLListCollection?


http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/XMLListCollection.html 

If data comes from List or ArrayList you should use ArrayCollection

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/ArrayCollection.html
Top Expert 2011

Commented:
Top Expert 2011
Commented:

Open Adobe SDK mx.controls.listClasses.ListBase class is parent class for DataGrid.
Check logic in dataProvider


public function set dataProvider(value:Object):void
    {
        if (collection)
        {
            collection.removeEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChangeHandler);
        }

        if (value is Array)
        {
            collection = new ArrayCollection(value as Array);
        }
        else if (value is ICollectionView)
        {
            collection = ICollectionView(value);
        }
        else if (value is IList)
        {
            collection = new ListCollectionView(IList(value));
        }
        else if (value is XMLList)
        {
            collection = new XMLListCollection(value as XMLList);
        }
       else if (value is XML)
        {
            var xl:XMLList = new XMLList();
            xl += value;
            collection = new XMLListCollection(xl);
        }
        else
        {
            // convert it to an array containing this one item
            var tmp:Array = [];
            if (value != null)
                tmp.push(value);
            collection = new ArrayCollection(tmp);
        }
Top Expert 2011

Commented:
So, if you will not create XMLListeCollection DataGrid will do that for you anyway