load text input control in Adobe flex dynamically? - apache-flex

<fx:Script>
<![CDATA[
import mx.controls.*;
import mx.controls.TextInput;
import mx.events.ListEvent;
public function init():void
{
for(var i:int=0;i<5;i++)
{
var txtbox:TextInput = new TextInput();
txtbox.id = "text"+i;
myHBox.addChild(txtbox);
}
}
public function getVal():void
{
}
]]>
</fx:Script>
<mx:HBox id="myHBox" width="100%" height="100%">
<mx:Button label="Get Value" click="getVal()"/>
</mx:HBox>
I have implemented this one. I am getting 5 textboxes with empty value, if i entered some value in each textbox then, i want to get specific 3rd textbox value wen some event trigger. so how i can i make. since am new to flex. Pls give me solutions. Thanks in advance.

Why don't you just store the values in its own data structure?
<fx:Script>
<![CDATA[
import mx.controls.*;
import mx.controls.TextInput;
import mx.events.ListEvent;
private var inputs:Vector.<TextInput> = new Vector.<TextInput>();
public function init():void
{
for(var i:uint = 0; i<5; i++)
{
var txtbox:TextInput = new TextInput();
inputs.push(txtbox);
myHBox.addChild(txtbox);
}
}
public function getVal():void
{
var value:String;
for(var i:uint = 0, len:uint = inputs.length; i<len; i++)
{
value += inputs[i].text + ' ';
}
trace(value);
}
]]>
</fx:Script>
<mx:HBox id="myHBox" width="100%" height="100%">
<mx:Button label="Get Value" click="getVal()"/>
</mx:HBox>
Also, if this is a new project, why are you using Flex 3?

Your question is not all too clear, but if I understand correctly,
Try this:
public function init():void
{
for(var i:int=0;i<5;i++)
{
var txtbox:TextInput = new TextInput();
//txtbox.id = "text"+i;
txtbox.name = "text"+i;
txtbox.addEventListener(Event.CHANGE,onChange);
myHBox.addChild(txtbox);
}
}
private function onChange(event:Event):void{
Alert.show(TextInput(event.target).text,TextInput(event.target).name + " Changed");
}
public function getVal():void
{
Alert.show(TextInput(myHBox.getChildByName("text3")).text,"Value");
}
cheers

Related

How to position spark List control below StageWebView in Flex Mobile

I am developing mobile application for iOS and Android using Flex Mobile. On one of the views I am displaying StageWebView with a Google map and a spark List control to display another data. I am using the StageWebView in order to benefit from Google Maps JavaScript API v3. Example code below:
<?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" title="Clubs" backgroundAlpha="0"
viewActivate="view1_viewActivateHandler(event)"
backKeyPressed="view1_backKeyPressedHandler(event)"
initialize="view1_initializeHandler(event)">
<fx:Script>
<![CDATA[
import flash.sensors.Geolocation;
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import spark.events.ViewNavigatorEvent;
private var myWebView:StageWebView;
[Bindable]
private var locations:ArrayCollection;
private var geolocation:Geolocation;
protected function view1_initializeHandler(event:FlexEvent):void
{
myWebView = new StageWebView();
myWebView.stage = this.stage;
}
protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
{
if(Geolocation.isSupported)
{
geolocation = new Geolocation();
geolocation.addEventListener(GeolocationEvent.UPDATE, onGeolocationChange);
}
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
super.updateDisplayList(unscaledWidth, unscaledHeight);
if (myWebView) {
var point:Point = (new Point());
point = localToGlobal(point);
myWebView.viewPort = new Rectangle(point.x,point.y, stage.width,stage.height/3);
}
}
protected function view1_backKeyPressedHandler(event:Event):void
{
if (myWebView)
{
myWebView.viewPort = null;
myWebView = null;
}
navigator.popView();
}
protected function onGeolocationChange(event:GeolocationEvent):void
{
geolocation.removeEventListener(GeolocationEvent.UPDATE, onGeolocationChange);
locations = new ArrayCollection();
var location0:Object = new Object();
location0.lat = event.latitude;
location0.long = event.longitude;
locations.addItem(location0);
var location1:Object = new Object();
location1.lat = "42.697325";
location1.long = "23.315364";
locations.addItem(location1);
var location2:Object = new Object();
location2.lat = "42.696441";
location2.long = "23.321028";
locations.addItem(location2);
var url:String = "http://whozzzin.dev.mediatecture.at/gmapsformobile/map.php";
var counter:Number = 1;
for each(var location:Object in locations)
{
if(counter == 1)
{
url += "?locations["+counter.toString()+"][lat] = " + location.lat;
url += "&locations["+counter.toString()+"][long] = " + location.long;
}
else
{
url += "&locations["+counter.toString()+"][lat] = " + location.lat;
url += "&locations["+counter.toString()+"][long] = " + location.long;
}
counter++;
}
myWebView.loadURL(url);
}
]]>
</fx:Script>
<s:navigationContent>
<s:Button includeInLayout="{Capabilities.version.indexOf('IOS') > -1}" visible="{Capabilities.version.indexOf('IOS') > -1}" id="backButton" label="BACK" click="view1_backKeyPressedHandler(event)"/>
</s:navigationContent>
<s:List width="100%" contentBackgroundAlpha="0" id="placesList" dataProvider="{locations}" labelField="lat">
<s:layout>
<s:TileLayout columnWidth="{(width - 16)/3}"/>
</s:layout>
</s:List>
</s:View>
Currently the list is not visible because it appears behind the StageWebView. My question is how to position the List control exactly after the WebStageView.
You can use the top constraint:
<s:List width="100%" top="{stage.height/3}" id="placesList"
Edit: Put in a trace(stage.height); in your updateDisplayList function and also look at stage.stageHeight. Those values should help you figure out the exact top value to use in this case.
How I solved it:
I get the bottom of the WebStageView and convert a new point from global to local with x =0 and y = myWebView.bootom:
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
super.updateDisplayList(unscaledWidth, unscaledHeight);
if (myWebView) {
var point:Point = (new Point(0,this.radiusDropDown.height));
point = localToGlobal(point);
myWebView.viewPort = new Rectangle(point.x,point.y, stage.width,stage.height/3);
this.placesList.y = globalToLocal(new Point(0,myWebView.viewPort.bottom)).y;
}
}

DataBinding in List

I am trying to bind data within a ArrayList to a list, but here only the last element shows up on the list(99), not the entire contents of the arraylist.
private function completeHandler(event:Event):void
{
var xmlData:XML = XML(event.target.data);
trace(xmlData);
var i:int = 0;
for (i;i<100;i++)
{
var arr:ArrayList = new ArrayList();
arr.addItem(i);
trace(arr);
}
list.dataProvider = arr;
}
I am not able to figure out what to do here?
You're creating an ArrayList with one item a 100 times. Replace with this and you should be fine:
var arr:ArrayList = new ArrayList();
for (var i:int = 0; i<100; i++) {
arr.addItem(i);
}
Or better yet, just wrap your XML in an XMLListCollection instead of copying the nodes one by one (assuming it is the actual content of the XML data you want instead of the indices):
private function completeHandler(event:Event):void
{
var xmlData:XML = XML(event.target.data);
list.dataProvider = new XMLListCollection(xmlData.children());
}
(Note that this is not DataBinding: it is just setting the dataProvider property)
check this code this will help you,
you can navigate through all data on the basis of rowcount of list....
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" minWidth="955" minHeight="600">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]private var _index:int = 0;
private var _coll:ArrayCollection = new ArrayCollection([{name:'ashish',age:'28'},{name:'abhi',age:'29'},{name:'kunal',age:'27'},
{name:'ashish1',age:'28'},{name:'abhi1',age:'29'},{name:'kunal1',age:'27'},
{name:'ashish2',age:'28'},{name:'abhi2',age:'29'},{name:'kunal2',age:'27'},
{name:'ashish3',age:'28'},{name:'abhi3',age:'29'},{name:'kunal3',age:'27'}]);
protected function button1_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
if((_index-li.rowCount>=0))
_index = _index - li.rowCount;
}
protected function button2_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
if((_index+li.rowCount<_coll.length))
_index = _index + li.rowCount;
}
]]>
</mx:Script>
<mx:List id="li" dataProvider="{_coll.source.slice(_index,(_index+li.rowCount))}" labelField="name" rowCount="3" width="100"/>
<mx:HBox>
<mx:Button label="<-" click="button1_clickHandler(event)"/>
<mx:Button label="->" click="button2_clickHandler(event)"/>
</mx:HBox>
</mx:Application>

Flex execution timeout in AdvancedDataGrid

I've a problem, I'm using an AdvancedDataGrid .
It loads about 3000 records with about 20 columns. I constantly get Flex execution timeout because the grid executes a lot inside LayoutManager.
How can I make it asyncronousely or faster at all?
Use Datagrid pagination.
http://gurufaction.blogspot.com/2007/02/flex-datagrid-paging-example-with.html
http://stackoverflow.com/questions/1893350/how-does-flex-3-datagrid-paging-work
or something like this
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="InitApp()" xmlns:MyComp="*">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.ItemClickEvent;
[Bindable]
public var myData:ArrayCollection = new ArrayCollection();
public var orgData:ArrayCollection = new ArrayCollection();
[Bindable]
public var nav:ArrayCollection = new ArrayCollection();
private var index:int = 0;
private var page:int = 25;
private var paging:Boolean = true;
private function InitApp():void {
for( var x:uint = 1; x <= 500; x++ ) {
var obj:Object = new Object();
obj.Id = x.toString();
obj.Name = "Column " + x.toString();
obj.Street = "5555 Street";
obj.Title = "CEO";
obj.City = "El Paso";
orgData.addItem(obj);
}
refreshDataProvider(0);
}
private function navPage(d:int):void {
index += d;
refreshDataProvider(index);
}
private function refreshDataProvider(start:int):void {
if (paging == true) {
page = 25;
myData = new ArrayCollection(orgData.source.slice(start,start+page));
}
else {
index = 0;
page = 500;
myData = new ArrayCollection(orgData.source);
}
down.enabled = index > 0;
up.enabled = index + page < orgData.length;
lbl.text = index.toString() + ' to ' + (index + page).toString() + ' of ' + orgData.length;
}
private function togglePaging():void {
paging = !paging;
toggle.label = paging.toString();
refreshDataProvider(index);
}
]]>
</mx:Script>
<mx:DataGrid id="dg" dataProvider="{myData}" width="510" height="202"></mx:DataGrid>
<mx:Button x="10" y="210" label="<" click="navPage(-25);" id="down"/>
<mx:Button x="216" y="210" label=">" click="navPage(25);" id="up"/>
<mx:Label x="58" y="212" text="page" width="150" id="lbl"/>
<mx:Button x="264" y="210" label="true" id="toggle" click="togglePaging();"/>
</mx:Application>
Load the data a chunk at a time? Say slightly more than however many rows are shown visually. That way you get the perceived performance that it renders quickly.
3000 records is a lot to be loading in one go it has to be said...

Flex + Image + Disable Drag

How do I diable the drag-drop of an image. I've tried to stopPropagation, but that didn't help.
Here is the snippet of the code that I've written
<mx:Image width="24" height="24" complete="init()" dragStart="disableMove(event)"
source="{(data.id==null)?'': (data.id.search('\\.') > 0) ? 'assets/icons/teacher.png' : 'assets/icons/student.png'}"
toolTip="{data.data}" doubleClick="itemDoubleClick(event, data.id)" doubleClickEnabled="true">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import flash.events.MouseEvent;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
private var allCurrentItems: Array = new Array();
private function itemDoubleClick(event: Event, id: String): void {
Alert.show("Clicked = "+id);
}
private function init(): void {
var menuLabel:String = "About School\u00A0";
var cm:ContextMenu = new ContextMenu();
cm.hideBuiltInItems();
var item:ContextMenuItem = new ContextMenuItem(menuLabel);
this.addEventListener(MouseEvent.MOUSE_DOWN, showClick);
//add eventlisteners to the menu item and provide functions
cm.customItems.push(item);
//cm.customItems = [item];
this.contextMenu = cm;
}
private function showClick(event:MouseEvent): void {
if (event.buttonDown) {
Alert.show(String(event.buttonDown));
}
}
private function disableMove(event: MouseEvent): void {
event.stopImmediatePropagation();
}
]]>
</mx:Script>
</mx:Image>
I got it, instead of calling disableMove(event) on dragStart(), I called it on mouseDown() it worked.

Change label of a NativeMenuItem

I'm experimenting with Adobe AIR and Google Maps API in Flex Builder. The question is, i'm making a NativeMenu, and was wondering, how can I change the label of the "Fullscreen" item to "Exit fullscreen" when the stage is in fullscreen?
If you see any thing in the code that could/should be written better, please let me know ;)
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
height="600"
width="700"
minHeight="100"
minWidth="100"
showStatusBar="false"
title="Gmaps 0.002"
>
<maps:Map
xmlns:maps="com.google.maps.*"
id="map"
mapevent_mapready="onMapReady(event)"
width="100%"
height="100%"
url=""
key=""
/>
<mx:Button id="fullscreenButton" click="toggleFullScreen()"/>
<mx:Script>
<![CDATA[
import com.google.maps.LatLng;
import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.MapType;
import com.google.maps.MapMouseEvent;
import com.google.maps.controls.MapTypeControl;
import com.google.maps.controls.ZoomControl;
import com.google.maps.controls.PositionControl;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.events.Event;
import mx.core.Window;
import flash.display.StageDisplayState;
private function onMapReady(event:Event):void {
map.setCenter(new LatLng(59.908165,10.742719), 14, MapType.NORMAL_MAP_TYPE);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyDownHandler(event:KeyboardEvent):void {
if (event.keyCode == 70) {
toggleFullScreen();
}
}
createMenu();
map.enableScrollWheelZoom();
map.enableContinuousZoom();
map.enableControlByKeyboard();
systemManager.stage.addEventListener(FullScreenEvent.FULL_SCREEN, fullScreenHandler);
map.addEventListener(MapMouseEvent.ROLL_OVER, function(event:MapMouseEvent):void {
map.addControl(new ZoomControl());
map.addControl(new PositionControl());
map.addControl(new MapTypeControl());
});
map.addEventListener(MapMouseEvent.ROLL_OUT, function(event:MapMouseEvent):void {
map.removeControl(new ZoomControl());
map.removeControl(new PositionControl());
map.removeControl(new MapTypeControl());
});
}
private function createMenu():void{
var mainMenu:NativeMenu = new NativeMenu();
var fullscreenMenu:NativeMenuItem = new NativeMenuItem("Fullscreen");
var maximizeMenu:NativeMenuItem = new NativeMenuItem("Maximize");
var restoreMenu:NativeMenuItem = new NativeMenuItem("Restore");
var separatorA:NativeMenuItem = new NativeMenuItem("A", true);
var closeMenu:NativeMenuItem = new NativeMenuItem("Close");
fullscreenMenu.addEventListener(Event.SELECT, handleMenuClick);
maximizeMenu.addEventListener(Event.SELECT, handleMenuClick);
restoreMenu.addEventListener(Event.SELECT, handleMenuClick);
closeMenu.addEventListener(Event.SELECT, handleMenuClick);
mainMenu.addItem(fullscreenMenu);
mainMenu.addItem(maximizeMenu);
mainMenu.addItem(restoreMenu);
mainMenu.addItem(separatorA);
mainMenu.addItem(closeMenu);
//fullscreenMenu.enabled = false;
//fullscreenMenu.label = "Test";
this.contextMenu=mainMenu;
}
private function handleMenuClick(e:Event):void{
var menuItem:NativeMenuItem = e.target as NativeMenuItem;
if(menuItem.label == "Fullscreen") toggleFullScreen();
if(menuItem.label == "Maximize") this.maximize();
if(menuItem.label == "Restore") this.restore();
if(menuItem.label == "Close") this.close();
}
private function fullScreenHandler(evt:FullScreenEvent):void {
if (evt.fullScreen) {
//fullscreenMenu.label = "Test";
} else {
}
}
private function toggleFullScreen():void {
try {
switch (systemManager.stage.displayState) {
case StageDisplayState.FULL_SCREEN_INTERACTIVE:
systemManager.stage.displayState = StageDisplayState.NORMAL;
break;
default:
systemManager.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
break;
}
} catch (err:SecurityError) {
// ignore
}
}
]]>
</mx:Script>
</mx:WindowedApplication>
in order to change a label into a NativeMenu, you can try this
yourNativeMenu.getItemAt(0).label="new_label";
where getItemAt(0) return the first item in the NativeMenu

Resources