ArrayCollection getItemAt - apache-flex

I created an Item Renderer for the Flex Spark List Component.
The problem is that this code will not work:
<s:Label left="10" right="10" top="10" bottom="10" fontSize="13" text="{data.getItemAt(1).toString()}"
textAlign="left" verticalAlign="middle"/>
While this code will:
<s:Label left="10" right="10" top="10" bottom="10" fontSize="13" text="{data[1].toString}"
textAlign="left" verticalAlign="middle"/>
I can't use the later code because of binding issues.
Does anyone have any idea on what I'm doing wrong?

As #J_A_X' comment stated, these are two completely different things.
{data.getItemAt(1)...
Refers to an ArrayCollection, or possibly some other collection.
{data[1]...
Refers to an Array.
Attempting to treat an array as an array collection will not get you very far. I would recommend ensuring that the each item in your list is actually an ArrayCollection. Alternatively, I would more strongly support replacing them with value objects, and binding to a 'title','text', or whatever property more correctly defines the text you wish to display.
{data.title}

Related

Flex DropDownList doesn't select item

I have an implementation for a drop-down list. All the values are there, however it doesn't allow the user to select an item with the mouse instead of just with the arrow keys.
<s:FormItem label="Food:">
<s:DropDownList id="dropDownList"
dataProvider="{foodList.lastResult.Food_Display_Table.Food_Display_Row}"
labelField="Display_Name"
/>
<s:DropDownList id="TEST">
<s:dataProvider>
<mx:ArrayList>
<fx:String>Red</fx:String>
<fx:String>Orange</fx:String>
<fx:String>Yellow</fx:String>
<fx:String>Blue</fx:String>
<fx:String>Green</fx:String>
</mx:ArrayList>
</s:dataProvider>
</s:DropDownList>
</s:FormItem>
</s:Form>
The first drop-down list is my actual implementation. The second one is a test. I have the same problem with both drop-down lists.
The DropdownList is not supported in the mobile device profile. There are some opensource implementations that add support for this, and they're quite good : https://www.flextras.com/index.cfm?event=ProductHome&productID=21

Flex components initialization and creation order

While trying to sort out a problem with loading components and firing events based on that, I came across a question for which I could not find an answer online.
Following is a sample of reference code
<s:WindowedApplication>
<s:states>
<s:State name="login"/>
<s:State name="data"/>
</s:states>
<s:VGroup id="login" includeIn="login">
<s:HGroup id="loginHGroup">
</s:HGroup>
</s:VGroup>
<s:VGroup id="data" includeIn="data">
<s:VGroup id="v1">
<s:HGroup id="h11">
</s:HGroup>
<s:HGroup id="h12">
</s:HGroup>
</s:VGroup>
<s:VGroup id="v2">
<s:HGroup id="h21">
</s:HGroup>
</s:VGroup>
</s:VGroup>
</s:WindowedApplication >
Now if I want to fire an event after the last UI component 'data' state gets created - which element do I fire it off from? Will HGroup h12 get created first or h21?
What is the flow of element creation in Flex - does it follow DOM schematics or is there some other mechanism?
[Remember that I cannot fire creationComplete at WindowedAPplication level as that is going to get fired when the login state is created].
Always the outer component is the last one to fire the creationcomplete event, so, you can add creation complete event listener on the application, or You can even add the applicationComplete event on the application which occurs after the creation complete.

Flex 4.6 push a specific view with a list item?

I am working on a Flex 4.6 Mobile project and am trying to use a list to navigate views from a menu view that has been set up.
I'm finding tutorials from Adobe, but most involve changing to the same view and just altering data based on which list item you clicked. What I am actually trying to do is , depending on which list item you click on, you get a unrelated view pushed in, different views completely per list item. Also, did I go about this the wrong way? I understand there are many different ways to navigate the views, with buttons, tabs, etc. But this is something that has been asked for. Example of my current list:
<s:List id="toolsList" x="10" y="284" width="255" height="197" borderVisible="false"
color="black" downColor="#00764C" fontSize="16" fontWeight="bold"
skinClass="view.skins.ListSkin" verticalScrollPolicy="off">
<s:itemRenderer>
<fx:Component>
<renderers:StyledIconItemRenderer labelField="label" iconField="icon"/>
</fx:Component>
</s:itemRenderer>
<s:ArrayCollection>
<fx:Object label="Settings" icon="#Embed('resources/arrow.png')"/>
<fx:Object label="Fault Current Search" icon="#Embed('resources/arrow.png')"/>
<fx:Object label="Share Picture" icon="#Embed('resources/arrow.png')"/>
<fx:Object label="System Info" icon="#Embed('resources/arrow.png')"/>
</s:ArrayCollection>
</s:List>
So, based on above example I would like to click on the "Settings" list item and get my settings view, then when I "pop" the settings view, I will go back to the menu and if I click the "Share Picture" list item, then I get that specific view, and so forth.
I am not really asking anyone to write code for me here, but maybe even point me in the right direction of a online example that maybe I haven't found yet.
Thank you in advance for any consideration
If you store some data on each item in the ArrayCollection about which view it should navigate to, then you could write a change event handler for the list that reads this data and will push the appropriate view.
For example, add the fully qualified class name of the view that each item should go to:
<s:ArrayCollection>
<fx:Object label="Settings" icon="#Embed('resources/arrow.png')" viewClass="com.whatever.SettingsClass/>
...
</s:ArrayCollection>
Then add a click handler to the list:
<s:List id="theList" change="onSelectedItemChange()" />
private function onSelectedItemChange():void
{
var className:String = theList.selectedItem.viewClass;
var viewClass:Class = getDefinitionByName(className) as Class;
ViewNavigatorApplication.navigator.pushView(viewClass);
}

How to use out-of-datagrid scope variable inside an ItemRenderer?

I'm binding an array of items to a data grid using ItemRenderer. I use the data variable to control the bindable data. I also have someComponentVariable that need be inserted into every row but its declared at the component scope, so the data grid doesn't seem to reconize it (compile error).
How can I use this variable (someComponentVariable) inside the ItemRenderer?
Code Example
<mx:DataGrid id="userBonusesGrid" width="100" height="248" showHeaders="false" wordWrap="true">
<mx:columns>
<mx:DataGridColumn headerText="" width="36">
<mx:itemRenderer>
<mx:Component>
<mx:VBox verticalAlign="middle" horizontalAlign="center">
<ns1:SidePanelBonus
bonusName="{data.name}" description="{data.description}"
arrow="{someComponentVariable}">
</ns1:SidePanelBonus>
</mx:VBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
If someComponentVariable is a public property of the class enclosing DataGrid, you can use outerDocument to access it from a component.
<ns1:SidePanelBonus bonusName="{data.name}" description="{data.description}"
arrow="{outerDocument.someComponentVariable}">
</ns1:SidePanelBonus>
See the "using the Component tag" section in Creating inline item renderers and editors for more info about outerDocument
No you can not use it at all. Each itemRenderer in data grid can only access the item for which the renderer was created. And this is done purposely because itemRendrers change dynamically, they are not bound for data forever, when you scroll, the items get scrolled not the renderers, they remain in same position or they might change, but corresponding item renderer's data always changes when you scroll. They dont share one to one relationship.
The only solution is to pass the data in the item in the form of some parent child relationship.

Recreating/resetting views with Flex ViewStack

I'm writing an Adobe AIR application using a ViewStack for the different application states. Is there a way to make sure that each view component is created/destroyed each time it is shown/hidden?
For instance, if I have a TextInput in a view, I want it to reset to its initial state each time I change to that view, rather than having previously entered text. Or, if I have a Timer, I want it to be destroyed when I leave the view so that it doesn't keep running when I'm in an unrelated part of the application. I know that I can manually initialize/destroy everything on the show() and hide() events, but is there an easier way?
AFAIK there is no built-in way to do this, so you'll have to do it manually by handling the show and hide events as you mention.
ViewStack does however have two methods "saveState" and "loadState" which could perhaps help you out with this. The history manager uses these methods to enable back/forward navigation. The docs don't seem to have any examples though.
ViewStacks can be the work of the devil when it comes to creation/deletion policies and managing state. We had all sorts of problems when we developed fiat ecoDrive and by about 3/4 of the way though we we're all very anti ViewStacks for the management of view state within our application.
However... a good bet would be to first set the creationPolicy to ContainerCreationPolicy.NONE. That way it's in your control as to when to create any of the panels in your ViewStack. Then i would think you would need to have some sort of logic so that as the ViewStack changes a panel it deletes or resets the one you were on.
Another viable alternative would be to use view states instead. Have a base state which acts as the main container and then a simple state for each of your sections. That way when you switch to a new state, the old state gets removed in reverse order to the way it was created. You do have to be disciplined with states though as they can end up getting really complex and messy when they start becoming nested. If you keep it simple it may work as you require.
In MXML 2009, you can use itemDestructionPolicy="auto" to destroy a component after use it. If you use this property in the first view component with two states (init, logged), you can destroy and reinitialize all child view components. Example :
<s:states>
<s:State name="init" />
<s:State name="logged" />
</s:states>
<s:SkinnableContainer id="skincon" width="100%" height="100%" backgroundAlpha="0"
backgroundColor="#FFFFFF">
<s:VGroup id="MainContainer" width="100%" height="100%" paddingTop="0"
paddingLeft="20" paddingRight="20" gap="0">
<views:_HeaderView id="header" />
<mx:ViewStack id="viewStack" width="100%" height="100%">
<s:NavigatorContent includeIn="init" itemDestructionPolicy="auto">
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
</s:layout>
<views:LoginView title="Login" id="loginView" />
</s:NavigatorContent>
<s:NavigatorContent includeIn="logged" itemDestructionPolicy="auto">
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="top" />
</s:layout>
<views:_CentralView id="userView" />
</s:NavigatorContent>
</mx:ViewStack>
<views:_FooterView id="footer" />
</s:VGroup>
</s:SkinnableContainer>
Both answers are correct -- there doesn't seem to be any built-in way to do it. I solved the problem by creating a "wrapper" component for my view component. It creates a new view component each time the view is shown. This isn't ideal, but fits my requirements nicely, and required few changes to my application structure.
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" show="init()" hide="cleanup()">
<mx:Script>
<![CDATA[
private var myComponent:MyComponent;
private function init():void
{
myComponent = new MyComponent();
componentContainer.addChild(myComponent);
}
private function cleanup():void
{
componentContainer.removeAllChildren();
}
]]>
</mx:Script>
<mx:Canvas width="100%" height="100%" id="componentContainer" />
</mx:Canvas>
Build your "views" as separate Modules, and then use the ViewStack to switch between them. You could then write a function to destroy the unused module(s) (check each module against the selectedChild property) when the ViewStack's "change" event is fired.
2ยข
I am using different states for my different views. On each state change i add and remove components.
This causes the add and remove events of UIComponent fire which allows me to initialize and cleanup my components each time they are added.
This is the idea...
<mx:states>
<mx:State name="state1">
<mx:AddChild>
<mx:SomeUIComponent id="myComp" add="myComp.initialize()" remove="myComp.cleanup()"/>
</mx:AddChild>
</mx:State>
</mx:states>

Resources