<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http:
backgroundColor="#FFFFFF"
backgroundAlpha="0"
horizontalAlign="left"
verticalGap="15" horizontalGap="15">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var myResult:XML;
[Bindable]
private var weatherObject:Object;
[Bindable]
private var listContents:ArrayCollection;
private namespace yweather = "http:;
use namespace yweather;
public function requestWeather():void {
weatherService.cancel();
var params:Object = new Object();
params.p = zip.text;
weatherService.send(params);
}
public function resultHandler(event:ResultEvent):void {
myResult = XML(event.result);
weatherObject = new Object();
weatherObject.zip = zip.text;
weatherObject.city = myResult.channel.yweather::location.@city;
weatherObject.temp = myResult.channel.item.yweather::condition.@temp;
weatherObject.imgsource = parseImageUrl(myResult.channel.item.description);
var a:Array = new Array(weatherObject);
listContents = new ArrayCollection(a);
}
private function parseImageUrl(fromHtml:XMLList):String {
var pattern:RegExp = /img src="(.+?)" /;
var results:Array = pattern.exec(fromHtml);
var imageURL:String = results[1]; return imageURL;
}
]]>
</mx:Script>
<mx:HTTPService
id="weatherService"
url="http:
resultFormat="e4x"
result="resultHandler(event)" />
<mx:Text text="1. Basic Form + HTTPService Retrieval" />
<mx:Form width="400">
<mx:FormItem label="Zip Code">
<mx:TextInput id="zip" />
<mx:Button label="Get Weather"
click="requestWeather()" />
</mx:FormItem>
</mx:Form>
<mx:Text text="Raw RSS Feed" />
<mx:TextArea id="resultFld"
text="{myResult}"
width="400" height="152" />
<mx:Text text="1. List w/ Image Item Renderer (Drop-In)" />
<mx:List dataProvider="{listContents}"
labelField="imgsource"
width="400" height="100"
itemRenderer="mx.controls.Image" />
<mx:Text text="2. List w/ HBoxWeatherDisplay itemRenderer" />
<mx:List dataProvider="{listContents}"
labelField="zip"
width="400" height="100"
itemRenderer="components.HBoxWeatherDisplay" />
</mx:Application>
The ItemRenederer should not set the values of the UI components inside the set data. The data should be written in the commitProperties method like so:
override protected function commitProperties():void
{
super.commitProperties();
zip.text = _data.zip;
city.text = _data.city;
temp.text = _data.temp + 'F';
img.source = _data.imgsource;
}
Otherwise you will get null exceptions if you try to enable drag and drop.