01-04-2011 03:54 PM
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;
}
}
}
Solved! Go to Solution.
01-04-2011 04:29 PM
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 ![]()
hope that helps. good luck!
01-05-2011 12:30 AM - edited 01-05-2011 12:31 AM
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
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!
01-05-2011 12:34 AM
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.
01-05-2011 01:38 AM
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
01-05-2011 01:38 AM
BTW JRab - do you ever sleep? ;-)
01-05-2011 01:41 AM
@p3pp3r: haha! i try not to. cause once i sleep i never wake up ![]()
01-05-2011 05:43 AM
JRab wrote:
@p3pp3r: haha! i try not to. cause once i sleep i never wake up
Amen, my favorite quote from rapper Nas:
"I don't sleep, 'cuz sleep is the cousin of death"
01-05-2011 11:21 AM
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.
01-05-2011 12:01 PM - edited 01-05-2011 12:02 PM
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.