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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Adobe AIR Development

Reply
Contributor
Cyriel
Posts: 19
Registered: ‎08-26-2008
Accepted Solution

Picker blues....

After having my previous picker problem solved here perfectly I unfortunately ran into another one with the picker. I have a simple picker wich alows you to select a date (shameleslly stolen from the api examples). I have embedded this in a new class. It display fine and you can select a date. Now I need to reeive this date back. So I have added a new function to my picker class which gets the indices, adds some numbers and sticks them into an arrray which it returns to the main program. Then all hell breaks loose. somehow it returns the numbers added to the picker itself. as I add 1900 to the year indice it crasches as it wil only go up to 249 (representing 2050). how come it does this? I was undr the impression that the getIndices method could just send me the indices and not always send them back to the picker as well. Is this another bug in the picker or am I doing something wrong here? Code pasted below. Part where it goes wrong is the GetDate() function which I call from the main program.

 

package classes
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import qnx.ui.data.DataProvider;
	import qnx.ui.picker.Picker;
	import qnx.ui.text.Label;
	
	[SWF(height="600", width="1024", frameRate="30", backgroundColor="#BBBBBB")]
	public class ClsDatePicker extends Sprite
	{
		
		public var picker:Picker;
		
		public function ClsDatePicker()
		{
			// Create three arrays to handle days, months, years.
			
			var arrDay:Array=[];
			var arrMonth:Array=[];
			var arrYear:Array=[];
			
			// Populate the month array and create the
			// day DataProvider 
			
			for (var i:int=1; i<32; i++) {
				arrDay.push({label: i.toString()});
			}
			
			var dayDP:DataProvider=new DataProvider(arrDay);
			
			
			// Populate and month array and create the month
			// DataProvider
			
			arrMonth.push({label: "January"});
			arrMonth.push({label: "February"});
			arrMonth.push({label: "March"});
			arrMonth.push({label: "April"});
			arrMonth.push({label: "May"});
			arrMonth.push({label: "June"});
			arrMonth.push({label: "July"});
			arrMonth.push({label: "August"});
			arrMonth.push({label: "September"});
			arrMonth.push({label: "October"});
			arrMonth.push({label: "November"});
			arrMonth.push({label: "December"});
			
			var monthDP:DataProvider=new DataProvider(arrMonth);
			
			// Create the year DataProvider
			
			for (var l:int=1900; l<2050; l++) {
				arrYear.push({label: l.toString()});
			}
			
			var yearDP:DataProvider=new DataProvider(arrYear);
			
			// Add the day, month and year DataProviders to the main
			// array
			
			var dpp:Array = new Array();
			
			dpp.push(dayDP);
			dpp.push(monthDP);
			dpp.push(yearDP);
			
			// Create the picker and add the main DataProvider
			
			picker = new Picker();
			
			picker.width=400;
			picker.height=31;
			picker.x=picker.y=300;
			picker.dataProvider=new DataProvider(dpp);
			addChild(picker);
			
			
			// To customize column widths,
			// all widths are added up and then used for  
			// a ratio against the component set width
			
			picker.setListWidth(0,100);
			picker.setListWidth(1,300);
			picker.setListWidth(2,200);
			
			// Set to Jan 1, 2010
			
			picker.selectedIndices=[0,0,110];
			
			// Add listeners for open, close and select events
			
			picker.addEventListener(Event.OPEN,handleOpen);
			picker.addEventListener(Event.CLOSE,handleClose);
			picker.addEventListener(Event.SELECT,handleSelect);
			picker.addEventListener(Event.CHANGE,handleChange);
			
			// A simple function that displays currently selected
			// item information in the console. 
			
			traceItems();
			
			
			
			function traceItems():void
			{
				for (var j:int = 0; j <picker.selectedItems.length; j++) {
					trace("Picker.selectedItems " +picker.selectedItems[j].label);
				}
				trace("Picker.selectedIndices "+picker.selectedIndices);
				trace("Picker.data "+picker.data);
			}
			function handleSelect(e:Event):void {
				trace("Picker.select event captured");
				
				traceItems();
			}
			
			function handleOpen(e:Event):void {
				trace("Picker.open event captured");
			}
			function handleClose(e:Event):void {
				trace("Picker.close event captured");
			}
			function handleChange(e:Event):void {
				trace("Picker.change event captured");
			}


		}
		public function getDate():Array
		{
			var arrDate:Array = new Array();
			arrDate = picker.selectedIndices;
			trace(arrDate[0] + arrDate[1] + arrDate[3]);
			arrDate[0] +=1;
			arrDate[1] +=1;
			arrDate[2] += 1900;
			return arrDate;
		}
	}
}

 

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

Re: Picker blues....

hey cyriel,

 

the way arrays work in AS3 is a little weird. When you do the array1 = array2 it simply copies the reference to the older array. so when you are making changes to your new and second array it makes changes to the first array. the work around for this is to create a "copy" of the array1 in this case the picker.selectedIndices. try the following code for your getDate() method:

 

 

public function getDate():Array
{
    var arrDate:Array = new Array();
    arrDate = picker.selectedIndices.concat();
    trace(arrDate[0] + ", "  + arrDate[1] + ", "  + arrDate[2]);
    
    arrDate[0] += 1;
    arrDate[1] += 1;
    arrDate[2] += 1900;


    return arrDate;
}

 

 I also changed your trace line because your original one had the numbers squished together. I have OCD :smileyvery-happy:

 

 

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
p3pp3r
Posts: 157
Registered: ‎12-16-2010
My Carrier: I carry it myself

Re: Picker blues....

[ Edited ]

 


JRab wrote:

hey cyriel,

 

the way arrays work in AS3 is a little weird. When you do the array1 = array2 it simply copies the reference to the older array. so when you are making changes to your new and second array it makes changes to the first array. the work around for this is to create a "copy" of the array1 in this case the picker.selectedIndices. 

 


 

Oh so this is by design? I think this applies to other types too (havent confirmed but I think var str2:smileyfrustrated:tring = str1 creates reference to str1 instead of copying)? I ran into this couple of times and thought I was doing something wrong or there was a bug!

 

----------
If you find this post helpful please "like" it and accept as a solution.
Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010

Re: Picker blues....

hey p3pp3r,

 

from what i can remember, only [Boolean, String, int, uint, and Number] data types pass by value. meaning they do indeed make a copy of themselves when doing str1 = str2 etc unlike an array. Everything else does it by reference in AS3 just like an array does.

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
p3pp3r
Posts: 157
Registered: ‎12-16-2010
My Carrier: I carry it myself

Re: Picker blues....

 


JRab wrote:

hey p3pp3r,

 

from what i can remember, only [Boolean, String, int, uint, and Number] data types pass by value. meaning they do indeed make a copy of themselves when doing str1 = str2 etc unlike an array. Everything else does it by reference in AS3 just like an array does.


Thanks for clarifying - I just checked and it was TextField in my code which was puzzling me for hours

 

----------
If you find this post helpful please "like" it and accept as a solution.
Please use plain text.
Developer
p3pp3r
Posts: 157
Registered: ‎12-16-2010
My Carrier: I carry it myself

Re: Picker blues....

BTW JRab - do you ever sleep? ;-)

----------
If you find this post helpful please "like" it and accept as a solution.
Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010

Re: Picker blues....

@p3pp3r: haha! i try not to. cause once i sleep i never wake up :smileyindifferent:

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.
Moderator
jakoby4204
Posts: 2,370
Registered: ‎12-02-2009

Re: Picker blues....

 


JRab wrote:

@p3pp3r: haha! i try not to. cause once i sleep i never wake up :smileyindifferent:


 

Amen, my favorite quote from rapper Nas:

 

"I don't sleep, 'cuz sleep is the cousin of death"

Please use plain text.
Contributor
Cyriel
Posts: 19
Registered: ‎08-26-2008

Re: Picker blues....

thank Jrab. You solved it for me yet again. Looks like I have to read up on actionscript a little to find out more about this tiny behaviour diferences between as and what I'm used to.

Please use plain text.
Developer
samasrinivas
Posts: 40
Registered: ‎02-13-2009

Re: Picker blues....

[ Edited ]

I concur with RJab, I was going nuts when I was passing around an array object to several functions and deep down within one of the function I was splicing the elements in the array thinking it was pass by value but eventually my original array was being effected resulting in data loss. So I have overcome the issue by simple keeping track of the index number I am currently processing without making any modifications to the array. But I have remembered going back to the good old pass by reference, which in this case has become pain in the butt.

Please use plain text.