I created a subclass of Event Class. I used the subclass in an itemrenderer, no error observed. But once I declared the itemrenderer to the List in the Main application, errors appears in the itemrenderer claiming "Type was not found or was not a compile-time constant: CustomDeleteEvent" and "Incorrect number of arguments: Expected no more than 1"
Please give me some advice. Thanks in advance.
In subclass :
package widgets.GetMap
{
import flash.events.Event;
public class CustomDeleteEvent extends Event
{
public static const DELETE_ITEM:String = "DELETE_ITEM";
public var deletedItem:String;
public function CustomDeleteEvent(type:String, deletedItem:String)
{
super(type);
this.deletedItem = deletedItem;
}
}
}
In ItemRenderer :
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer name="CustomItemRen"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="true"
minHeight="24">
<s:states>
<s:State name="normal" />
<s:State name="hovered" />
<s:State name="selected" />
</s:states>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.CloseEvent;
import spark.components.List;
protected function deleteHandler():void
{
var deleteItem:String = itemIndex.toString();
var tryevent:CustomDeleteEvent;
owner.dispatchEvent(tryevent,deleteItem);
Object(owner).dataProvider.removeItemAt(itemIndex);
}
]]>
</fx:Script>
<s:HGroup width="100%" height="100%"
verticalAlign="middle"
paddingLeft="2" paddingRight="2"
paddingTop="2" paddingBottom="2">
<s:Label id="lbl" text="{data.toString()}" width="100%" color="#30FF00"/>
<s:Button id="btn" includeIn="hovered,selected" y="-16" width="35" height="22" label="X"
accentColor="#FFFFFF" color="#FF0000" fontFamily="Verdana" fontSize="12"
fontWeight="bold" mouseDown="deleteHandler();" toolTip="Delete item"/>
</s:HGroup>
</s:ItemRenderer>
In the main application:
<s:Application name="Spark_List_itemRenderer_hovered_test"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Style source="test.css"/>
<s:List id="lst"
itemRenderer="CustomItemRen"
width="300"
horizontalCenter="0" verticalCenter="0">
<s:layout>
<s:VerticalLayout gap="0"
horizontalAlign="justify"
requestedRowCount="8" />
</s:layout>
<s:dataProvider>
<s:ArrayList>
<fx:Object label="Application" />
<fx:Object label="Label" />
<fx:Object label="List" />
</s:ArrayList>
</s:dataProvider>
</s:List>
</s:Application>
In this line, you didn't initialize the event object ("tryEvent" like new CustomDeleteEvent())..
and also in your "CustomDeleteEvent"..there is "type" parameter, which is not in "tryEvent" object...
var tryevent:CustomDeleteEvent;
owner.dispatchEvent(tryevent,deleteItem);
Use below code for dispatch the event....
owner.dispatchEvent(new CustomDeleteEvent(CustomDeleteEvent.DELETE_ITEM, deleteItem));
hope this will help you....
Related
I have tried to do this many ways but for the life of me cannot think how to. Basically I have a list. When an item on the list is selected the radio button labels change. However, I want a label and text area to appear saying once the user has clicked on the radio button if it is right or not.
Code so far: -
<s:VGroup x="103" y="130" width="123" height="125">
<s:RadioButton id="RadioButton1" label="{data.QuestionsRadioButton1}" groupName="QuestionsTestRadioButtons" click="RadioButton1_clickHandler(event)"/>
<s:RadioButton label="{data.QuestionsRadioButton2}" groupName="QuestionsTestRadioButtons" click="radiobutton1_clickHandler(event)" />
<s:RadioButton id="RadioButton3" label="{data.QuestionsRadioButton3}" groupName="QuestionsTestRadioButtons" click="radiobutton2_clickHandler(event)"/>
</s:VGroup>
I will not post all the code as we will be here forever. However, is there a way maybe an if function? To say if the radio button clicked actually is the right answer or not?
Any suggestions would be helpful
Thank You
My attempt to draft a complete example:
Questionaire.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:local="*">
<fx:Declarations>
<s:ArrayCollection id="questions">
<local:Question text="It is raining …">
<s:ArrayCollection>
<local:Answer text="Men"/>
<local:Answer text="Cats and dogs" correct="true"/>
<local:Answer text="Candy"/>
</s:ArrayCollection>
</local:Question>
<local:Question text="The sky is …">
<s:ArrayCollection>
<local:Answer text="Blue" correct="true"/>
<local:Answer text="Orange" correct="true"/>
<local:Answer text="Grey" correct="true"/>
<local:Answer text="Green"/>
</s:ArrayCollection>
</local:Question>
</s:ArrayCollection>
</fx:Declarations>
<s:DataGroup dataProvider="{questions}" itemRenderer="QuestionRenderer">
<s:layout>
<s:VerticalLayout gap="24"/>
</s:layout>
</s:DataGroup>
</s:Application>
QuestionRenderer.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:DataRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
[Bindable("dataChange")]
public function get question():Question {
return data as Question;
}
]]>
</fx:Script>
<fx:Declarations>
<s:RadioButtonGroup id="answerGroup"/>
</fx:Declarations>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Label fontWeight="bold" text="{question.text}"/>
<s:DataGroup dataProvider="{question.answers}">
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<s:itemRenderer>
<fx:Component>
<s:DataRenderer>
<fx:Script>
<![CDATA[
import spark.components.RadioButtonGroup;
public function get answerGroup():RadioButtonGroup {
return outerDocument.answerGroup;
}
]]>
</fx:Script>
<s:RadioButton groupName="answerGroup" label="{data.text}" value="{data}"/>
</s:DataRenderer>
</fx:Component>
</s:itemRenderer>
</s:DataGroup>
<s:Label visible="{answerGroup.selectedValue}" text="This is {answerGroup.selectedValue.correct ? 'correct' : 'incorrect'}."/>
</s:DataRenderer>
Question.as
package {
import mx.collections.ArrayCollection;
[Bindable]
[DefaultProperty("answers")]
public class Question {
public var text:String;
public var answers:ArrayCollection;
}
}
Answer.as
package {
[Bindable]
public class Answer {
public var text:String;
public var correct:Boolean = false;
}
}
I'm writing a very simple flex application with mxml. I have many buttons, when I click one of them, I hope its value changes to World.
My code is:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
private function hello():void {
this.label = "World!";
}
]]>
</fx:Script>
<mx:HBox>
<s:Button click="hello()" label="Hello" />
<s:Button click="hello()" label="Hello" />
<s:Button click="hello()" label="Hello" />
<s:Button click="hello()" label="Hello" />
<s:Button click="hello()" label="Hello" />
</mx:HBox>
</s:Application>
Which is incorrect since this.label = "World!" can't be compiled that this.label is not found.
How to let the this reference to the button I clicked, or how to implement this ?
Try below code this may help you: -
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
private function hello(event:MouseEvent):void {
event.currentTarget.label = "World!";
}
]]>
</fx:Script>
<mx:HBox>
<s:Button click="hello(event)" label="Hello" />
<s:Button click="hello(event)" label="Hello" />
<s:Button click="hello(event)" label="Hello" />
<s:Button click="hello(event)" label="Hello" />
<s:Button click="hello(event)" label="Hello" />
</mx:HBox>
</s:Application>
sorry for my poor English.
I seached for hours but I couldn't find any solution for my problem.
I want to navigate my view to another from getting name of view from arrayList in DataProvider.
How can I get the clicked item's url and navigate as View.
Thanks..
<s:List id="listMenu" top="0" bottom="0" left="0" right="0" height="100%" width="100%"
click="navigator.pushView(listMenu.url as View)">
<s:dataProvider>
<s:ArrayList>
<fx:Object name="Title1" detail="Detail1" src="#Embed('../media/graphics/credit-card.png')" url="View1" />
<fx:Object name="Title2 Sorgulama" detail="Detail2" src="#Embed('../media/graphics/IMEI.png')" url="View2" />
</s:ArrayList>
</s:dataProvider>
<s:itemRenderer>
<fx:Component>
<s:IconItemRenderer labelField="name" messageField="detail" iconField="src" iconWidth="64" iconHeight="64" height="68" />
</fx:Component>
</s:itemRenderer>
Here you go -
assets/icons/FB.png:
assets/icons/GG.png:
views/Home.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
import mx.core.BitmapAsset;
import mx.events.FlexEvent;
import spark.components.SplitViewNavigator;
import spark.components.ViewNavigator;
import spark.core.SpriteVisualElement;
import spark.events.IndexChangeEvent;
[Embed(source="assets/icons/FB.png")]
private static const FB_CLASS:Class;
[Embed(source="assets/icons/GG.png")]
private static const GG_CLASS:Class;
private static const FB_ICON:BitmapAsset = new FB_CLASS() as BitmapAsset;
private static const GG_ICON:BitmapAsset = new GG_CLASS() as BitmapAsset;
protected function handleChange(event:IndexChangeEvent):void {
var item:Object = login.selectedItem;
navigator.pushView(item.view, item.str);
}
]]>
</fx:Script>
<s:List width="100%" height="100%" id="login" change="handleChange(event);">
<s:dataProvider>
<s:ArrayCollection>
<fx:Object icon="{FB_ICON}" view="{FB}" label="Play at" msg="Facebook.com" />
<fx:Object icon="{GG_ICON}" view="{GG}" label="Play at" msg="Google+" />
</s:ArrayCollection>
</s:dataProvider>
<s:itemRenderer>
<fx:Component>
<s:IconItemRenderer labelField="label" iconField="icon" messageField="msg"/>
</fx:Component>
</s:itemRenderer>
</s:List>
</s:View>
App.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
firstView="views.Home">
</s:ViewNavigatorApplication>
The views/FB.mxml and views/GG.mxml referenced above - to be created by yourself ;-)
I have a TabNavigator, and each tab is a Module. One of the modules is labelled Units and the full code of the module is posted in this post.
There are several problems:
1) Forms are not populated with data from the datagrid selection.
2) Selecting a row and clicking delete gives the very-common error: TypeError: Error #1009: Cannot access a property or method of a null object reference.
A trace on the valueObject unit within the selectionChangeHandler function gives NULL. Why?
Note: In other modules (other tabs of the TabNavigator), I have DropDownLists populated with units. This means that the valueObject Unit is defined in the other modules. However, valueObjects should be private to modules, and not shared. I am unsure where the problem comes.
Full module code:
<?xml version="1.0" encoding="utf-8"?>
<s:Module xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:unitservice="services.unitservice.*"
xmlns:valueObjects="valueObjects.*"
width="724"
height="674">
<fx:Style source="assets/CAaNDFlex.css"/>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
import spark.events.GridSelectionEvent;
protected function unitsDg_creationCompleteHandler(event:FlexEvent):void
{
getUnitsResult.token=unitservice.getUnits();
}
protected function addBtn_clickHandler(event:MouseEvent):void
{
currentState="unitsAdd";
unit=new Unit();
}
protected function unitsDg_selectionChangeHandler(event:GridSelectionEvent):void
{
trace(event.currentTarget.selectedItem); //Unit object detected
trace(event.currentTarget.selectedItem as Unit); //NULL
trace(unit); // unit is NULL. Why?
currentState="unitsDetails";
}
protected function button_clickHandler(event:MouseEvent):void
{
trace(unit); // unit is NULL. Why?
unit.unitName=unitNameTextInput.text;
if (unit.unitID == 0)
{
createUnitResult.token=unitservice.createUnit(unit);
}
else
{
updateUnitResult.token=unitservice.updateUnit(unit);
}
}
protected function updateBtn_clickHandler(event:MouseEvent):void
{
currentState="unitsUpdate";
}
protected function createUnitResult_resultHandler(event:ResultEvent):void
{
currentState="unitsDetails";
unit.unitID=event.result as int;
unitsDg.dataProvider.addItem(unit);
unitsDg.setSelectedIndex(unitsDg.dataProvider.getItemIndex(unit));
unitsDg.ensureCellIsVisible(unitsDg.selectedIndex);
}
protected function deleteBtn_clickHandler(event:MouseEvent):void
{
deleteUnitResult.token = unitservice.deleteUnit(unit.unitID);
}
protected function deleteUnitResult_resultHandler(event:ResultEvent):void
{
unitsDg.dataProvider.removeItemAt(unitsDg.selectedIndex);
currentState="units";
}
]]>
</fx:Script>
<s:states>
<s:State name="units"/>
<s:State name="unitsDetails"/>
<s:State name="unitsAdd"/>
<s:State name="unitsUpdate"/>
</s:states>
<fx:Declarations>
<s:CallResponder id="getUnitsResult"
result="unit = getUnitsResult.lastResult as Unit"/>
<unitservice:UnitService id="unitservice"
fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
showBusyCursor="true"/>
<valueObjects:Unit id="unit" />
<s:CallResponder id="createUnitResult"
result="createUnitResult_resultHandler(event)"/>
<s:CallResponder id="updateUnitResult"/>
<s:CallResponder id="deleteUnitResult" result="deleteUnitResult_resultHandler(event)"/>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Binding destination="unit" source="unitsDg.selectedItem as Unit"/>
<s:DataGrid id="unitsDg" x="10" y="37"
creationComplete="unitsDg_creationCompleteHandler(event)" requestedRowCount="4"
selectionChange="unitsDg_selectionChangeHandler(event)">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="unitName"
headerText="unitName">
</s:GridColumn>
<s:GridColumn dataField="unitID"
headerText="unitID">
</s:GridColumn>
</s:ArrayList>
</s:columns>
<s:typicalItem>
<fx:Object unitID="unitID1"
unitName="unitName1">
</fx:Object>
</s:typicalItem>
<s:AsyncListView list="{getUnitsResult.lastResult}"/>
</s:DataGrid>
<s:Button id="addBtn" x="10" y="0" label="Add" click="addBtn_clickHandler(event)"
styleName="actionButton"/>
<s:Form includeIn="unitsAdd,unitsUpdate"
x="10"
y="176"
defaultButton="{button}">
<s:FormItem label="unitName">
<s:TextInput id="unitNameTextInput"
text="{unit.unitName}"/>
</s:FormItem>
<s:Button id="button"
label="Add"
click="button_clickHandler(event)"
label.unitsUpdate="Update"/>
</s:Form>
<s:Button id="updateBtn" x="138" y="0" label="Update" click="updateBtn_clickHandler(event)"/>
<s:Button id="deleteBtn" x="266" y="0" label="Delete" click="deleteBtn_clickHandler(event)"/>
<s:Form includeIn="unitsDetails" x="10" y="176">
<s:FormItem label="unitName">
<s:Label id="unitNameLabel" text="{unit.unitName}"/>
</s:FormItem>
</s:Form>
</s:Module>
The selectedObject is not successfully casting to Unit, which means it probably wasn't Unit or a subclass prior to the cast. Casting it to Unit won't make it a Unit unless it was one before.
i'm trying to populate a <mx:List> with a dataProvider that i received from a soap request.
i get the following error when i try to achieve this:
TypeError: Error #1034: Type Coercion failed: cannot convert AchivementsItemRenderer#1906a851 to mx.controls.listClasses.IListItemRenderer.
at mx.controls::List/createItemRenderer()[E:\dev\gumbo_beta2\frameworks\projects\framework\src\mx\controls\List.as:1987]
at mx.controls::List/http://www.adobe.com/2006/flex/mx/internal::getMeasuringRenderer()[E:\dev\gumbo_beta2\frameworks\projects\framework\src\mx\controls\List.as:1896]
at mx.controls::List/commitProperties()[E:\dev\gumbo_beta2\frameworks\projects\framework\src\mx\controls\List.as:911]
at mx.core::UIComponent/validateProperties()[E:\dev\gumbo_beta2\frameworks\projects\framework\src\mx\core\UIComponent.as:7387]
at mx.managers::LayoutManager/validateProperties()[E:\dev\gumbo_beta2\frameworks\projects\framework\src\mx\managers\LayoutManager.as:572]
at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\gumbo_beta2\frameworks\projects\framework\src\mx\managers\LayoutManager.as:700]
at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\gumbo_beta2\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1069]
if i use that same dataProvider on a <mx:Repeater> it works without any problems.
what am i missing?
mxml file
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
xmlns:local="*" backgroundColor="#aabbcc" fontSize="11" creationComplete="init()">
`
public var soap:SoapMyService = new SoapMyService();
[Bindable]
public var myData:Array;
public function handleSoapResult(event:GetMyResultEvent):void {
myData=event.result;
}
public function updateData():void {
soap.getData(528);
}
public function init():void {
soap.addgetMyDataEventListener(handleSoapResult);
updateData();
}
]]>
`
<mx:List id="myList" height="157" width="160"
itemRenderer="theItemRenderer"
baseColor="#323232" dataProvider="{myData}" >
</mx:List>
</mx:VBox>
item renderer
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
xmlns:local="*">
<s:states>
<s:State name="normal"/>
<s:State name="hovered"/>
<s:State name="selected" />
</s:states>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:HGroup verticalAlign="middle" paddingTop="0" paddingBottom="0">
<mx:Image source="http://www.xpogames.com/{data.img}" width="50" height="40" alpha.hovered=".5"/>
<s:Label text="{data.username}" color.hovered="0x1313cd" color.selected="0x000000" verticalAlign="bottom"/>
</s:HGroup>
</s:ItemRenderer>
i resolved the issue by using <s:List> instead of <mx:List> and by inserting event.result into an ArrayCollection. thanks for your help! :)