开发者

How to create a DataProvider using specific elements from an XML object in Flash AS3

I have this XML (Flash/AS3):

 <channel>
 <title>...</title>
 <description>...</description>
 <item><summary>...</summary><detail>...</detail></item>
 <item><summary>...</summary><detail>...</detail></item>
 ...
 </channel>

I want to create a DataProvider containing the elements (for use in a datagrid).

I thought this would work:

var items:XML = new XML(evt.target.data); //url loader event listener 'complete'
trace(items..item is XMLList); // true
myDP = new DataProvider(items..item);

But I get this error:

 TypeError: 开发者_运维百科Error: Type Coercion failed: cannot convert 
   <item><summary>...</summary><detail>...</detail></item>
   <item><summary>...</summary><detail>...</detail></item>
     ...
 to Array or DataProvider.
    at fl.data::DataProvider/getDataFromObject()
    at fl.data::DataProvider()

What am I doing wrong?


You have to give the DataProvider an XML, not an XMLList:

var items:XML = <channel>
   <title>Title</title>
   <description>Description</description>
   <item><summary>Summary 1</summary><detail>Detail 1</detail></item>
   <item><summary>Summary 2</summary><detail>Detail 2</detail></item>
</channel>;

var xml:XML = <dummy/>;
xml.appendChild(items..item);
list.dataProvider = new DataProvider(xml);
list.labelFunction = function(item:Object) {
   return item.summary;
}


You don't need to create an instance of DataProvider. You should be able to directly assign the dataProvider property with the XML instance. The dataProvider setter will automatically determine the object type and handle it appropriately.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜