dataProvider d'une ComboBox via Httpservice
Bonjour,
Actuellement, je cherche à alimenter une combox box depuis un fichier XML mais je n'y arrive pas malgré mes recherches sur les forums, pouvez vous m'aider :
mon fichier XML :
Code:
1 2 3 4 5 6 7 8
| <rooms>
<room>
<label>foo 1</label>
</room>
<room>
<label>foo 2</label>
</room>
</rooms> |
dans mon fichier mxml (le principal), j'ai mis entre autre :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="init()">
<!-- utilisation de httpService pour charger les données de la ComboBox
================================================================================-->
<mx:Script>
<![CDATA[
// Importation des class
import mx.events.ListEvent;
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
//Declaration des variables
[Bindable] private var roomList:XMLList=
<>
<label>Item 1</label>
<label>Item 2</label>
</>;
private function init():void
{
rooms.send();
}
private function httpFaultHandler(event:FaultEvent):void
{
Alert.show("There was a problem","Error");
}
private function httpResultHandler(event:ResultEvent):void
{
roomList = event.result.rooms.room;
combo3.dataProvider = event.result.rooms.room;
}
]]>
</mx:Script>
<mx:HTTPService id="rooms" url="assets/roomList.xml?cache=null1"
fault="httpFaultHandler(event)"
result="httpResultHandler(event)"/>
<!--===================================================
beginning of form
=======================================================-->
<mx:FormHeading label="three methodes to provide a ComboBox"/>
<mx:Form>
<mx:ComboBox id="combo1" width="210" x="190" y="34" >
<mx:dataProvider>
<mx:String>Option 1</mx:String>
<mx:String>Option 2</mx:String>
<!-- Add all other options. -->
</mx:dataProvider>
</mx:ComboBox>
<mx:ComboBox id="combo2" width="210" x="190" y="34" dataProvider="{roomList}" />
<mx:ComboBox id="combo3" width="210" x="190" y="34" dataProvider="{rooms.rooms.name}"/>
</mx:Form>
</mx:Application> |
et j'ai une erreur lors de mon groupement de combo box (mon dernier bloc souligné), il m'affiche :
Citation:
1119: Accès à la propriété rooms peut-être non définie, via la référence de type static mx.rpc.http.mxml:HTTPService.
Je ne sais pas comment résoudre mon erreur encore, si quelqu'un pouvait m'aider?
merci
clio:king:
C'est sûr ! un code claire ça aide :P
Hello,
Après avoir simplifier ton code je trouve deux erreurs, écrite en rouge dans le code. Il faut supprimer ces lignes.
La premier erreur est une erreur de compilation. C'est flex qui nous le dit ( 1119: Accès à la propriété ... )
L'autre erreur est un erreur de transtypage pendant l'exécution. roomList est un XMLList alors que event.result.rooms.room est un ArrayCollection.
Cette derniere ligne :
Code:
combo3.dataProvider = event.result.rooms.room;
permet de nourrir la ComboBox du Xml.
Crdl,