1

i get this error when i retrieve an XML that only has 1 node (no repeating nodes) and i try to store in an ArrayCollection. -When I have MORE than 1 "name" nodes...i do NOT get an error. My test show that XMLListCollection does NOT work either.

TypeError: Error #1034: Type Coercion failed: cannot convert "XXXXXX" to mx.collections.ArrayCollection.

this error occurs as the line of code:

myList= e.result.list.name;

Why can't ArrayCollection work with a single node? I'm using this ArrayCollection as a dataprovider for a Component -is there an alternative I can use that will take BOTH single and repeating nodes as well as work as a dataprovider? Thanks in advance!

code:

[Bindable]
private var myList:ArrayCollection= new ArrayCollection();

        private function getList(e:Event):void{

            var getStudyLoungesService:HTTPService = new HTTPService();
            getStuffService.url = "website.com/asdf.php";
            getStuffService.addEventListener(ResultEvent.RESULT, onGetList);
            getStuffService.send();

        }

        private function onGetList(e:ResultEvent):void{

            myList= e.result.list.name;
        }
4

4 に答える 4

1

ここでの問題は、行が1つしかない場合、結果をarrayCollectionとして使用できないため、Flexが文句を言うことです。

私の回避策は、返すデータを含む行数をXMLに入れることでした。たとえば、次のようにしました。

<list><nr_rows>3</nr_rows><name>...</name><name>...</name><name>...</name></list>

したがって、結果が返ってきたら、取得している行数を確認します(MySQLクエリから返された行数はで取得できますmysql_num_rows

e.result.list.nr_rows

したがって、これが1つの場合は、オブジェクトをarrayCollectionに追加します。複数ある場合は、結果を使用してACと等しくすることができます(この場合、プロジェクトはACです)。

if (event.result.list.nr_rows == '1'){
    myList.addItem(event.result.list.name);
} else {
    myList = event.result.list.name;
}
于 2010-05-20T06:59:28.280 に答える
0

If you are not particular about getting the final result onto a ArrayCollection, you can do the following to get the final result onto an XMLList or XMLListCollection.

1) set the resultFormat property of HTTPService to e4x.

2) Do not mention the root xml tag name while referencing it. Reference the output as: myList:XMLList = e.result.name.

This works whether the retrieved XML has one or more than one elements.

于 2011-07-07T10:52:23.667 に答える
0

I just had this problem today and it led me to this question. I'm not sure if your use-case is exactly the same as mine, but you might want to try showRoot="true" on the mx:Tree. It seems to force the root node to be shown when there is only one item, and gets ignored with multiple items.

于 2013-05-08T21:22:11.607 に答える
-1

add a row object to get the row of the XML node can work, but I think some better method have be, is ActionScript is as powerful as Java?

于 2010-11-03T03:55:11.433 に答える