Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
New Contributor
kanishkprasad
Posts: 7
Registered: ‎02-10-2011
My Device: Playbook
My Carrier: Developer
Accepted Solution

Getting a 0 when converting text input into a number. need help.

Hi everyone,

I am a 2nd year CS student designing my 1st app for blackberry. I'm using the start up code provided by RIM as my basis.

 

My problem is that when I convert a text input into a number using parseInt(foo.text) or Number(foo.text) I get a 0 instead of the number entered by the user. Here is the code I have written for user input:

 

var myInput:TextField = new TextField();
            addChild(myInput);
            
            myInput.type = "input";
            myInput.border = true;
            myInput.x = 140;
            myInput.y = 40;
            myInput.width = 125;
            myInput.height = 20;

var n:Number = Number(myInput.text);

//          var n:smileyfrustrated:tring = String(myInput.text);

//          var firstnum = Number(n);
            trace(n);

 

the trace function returns a 0. I have also tried this:

 

var firstnum:TextInput = new TextInput();
            newUfirstnum.width = 200;
            newfirstnum.x = 140;
            newUfirstnum.y = 65;
            newfirstnum.height = 20;
            addChild(firstnum);

var input:int = parseInt(firstnum.text);

trace(input);

 

Does anyone know why I am getting a 0 and what I can do to fix it? Any advice would help.

 

Thank you.

 

 

Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Getting a 0 when converting text input into a number. need help.

hey,

 

from the looks of it you are trying to retreive the text from the textinput object too early. you are storing the variable with the converted text of "" since at first hte TextInput object is in fact blank when it loads up.

 

what you need to do is in whatever function you are using for the "submission" of when the user is done typing or clicks a button is retreive and store the value of the text input and convert it then. this would most likely be done in your listener funciton when a user hits "submit" or something.

 

hope that helps. good luck!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Getting a 0 when converting text input into a number. need help.

hey,

 

just a follow up in case you are having trouble, here is a working application that uses the same variables you did. it involves a TextInput object (QNX) and a label button that you click on to see what the variable text is in the TextInput object. when you click it will trace the value into the debugger:

 

 

package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	
	import qnx.ui.buttons.LabelButton;
	import qnx.ui.text.TextInput;
	
	[SWF(width="1024",height="600",backgroundColor="#CCCCCC",frameRate="30")]
	public class TextInputTest extends Sprite
	{
		/**
		 * 
		 * Place your variables outside of the constructor
		 * it makes it easier to work with when using
		 * other functions like listener functions
		 * 
		 */
		private var firstnum:TextInput;
		private var myBtn:LabelButton;
		
		public function TextInputTest()
		{
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			/**
			 * Create your TextInput object just as you did
			 * before, only this time we wont try to trace
			 * the data in it right away
			 */
			
			firstnum = new TextInput();
			
			firstnum.width = 200;
			firstnum.x = 140;
			firstnum.y = 65;
			firstnum.height = 52;
			
			addChild(firstnum);
			
			/**
			 * We are going to create a label button
			 * that allows us to listen for a MouseEvent.CLICK
			 * (mouse click) that will tell the application
			 * to trace out our data
			 * 
			 */
			
			myBtn = new LabelButton();
			myBtn.label = "Trace!";
			myBtn.setSize(150,52);
			myBtn.setPosition(375, 65);
			
			/**
			 * This is the important line, to add a listener. We
			 * tell the application to add a mouse click event 
			 * listener and we will run the onClick function when
			 * there is a click
			 *
			 */
			
			myBtn.addEventListener(MouseEvent.CLICK, onClick);
			
			addChild(myBtn);
		}
		
		private function onClick(e:MouseEvent):void
		{
			/**
			 * 
			 * here is where we will do our conversion and 
			 * output via the trace function
			 * 
			 */
			
			var input:int = parseInt(firstnum.text);
			
			trace(input);
		}
		
	}
}

 

I've inserted comments to help you along the way. also i want you to note that ActionScript (Adobe AIR) is all event based. so you have to use events and listeners to make things happen. hope that helps a little more. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.
New Contributor
kanishkprasad
Posts: 7
Registered: ‎02-10-2011
My Device: Playbook
My Carrier: Developer

Re: Getting a 0 when converting text input into a number. need help.

Thank you for the quick reply. The code you wrote helped a lot, I had been stuck on this problem for about a week. I dont think I can thank you enough for this :smileyhappy:

Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Getting a 0 when converting text input into a number. need help.

hey,

 

dont sweat it! just glad you got it figured out :smileyhappy:

 

should you have anymore questions, be sure to search the forums - its full of other valuable information for developing on the playbook. if you cant find what you are looking for just make a thread like you did now and we'll help you figure it out! good luck on the rest of your app development!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.