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
Developer
kaputafj
Posts: 57
Registered: ‎05-04-2009
My Device: 9550
Accepted Solution

DropDown selection

Has anyone been able to successfully get the current selection from a DropDown?  I've got the DropDown loaded with Strings, and can change the selection in the simulator. 

 

                var temp3:smileyfrustrated:tring = myDropDown.selectedItem.toString();

 

doesn't work. 

However,

               var temp1:int = myDropDown.selectedIndex;

 

will correctly return the index of the selected item. 

 

I feel like I'm missing something obvious here. 

Thanks in advance for any help.

Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: DropDown selection

hey,

 

you can set up your data provider as an array and then simply get the selected index of the chosen drop down item and then using that call the item from the array. for example here is a list of colors in a drop down list:

 

 

var listOfThings:DropDown = new DropDown();

var dp:DataProvider = new DataProvider();

var arr:Array = new Array([{label:'green'},
	{label:'blue'},
	{label:'orange'},
	{label:'red'},
	{label:'yellow'},
	{label:'silver'}] );

dp.setItems(arr);

listOfThings.dataProvider = dp; 

 

 

now if you were to get the selected index of the drop down item and then reference the array at that index:

 

 

arr[listOfThings.selectedIndex].label;

 this should retreive the label of the selected item.

 

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
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: DropDown selection

[ Edited ]

Alternative to JRab excellent example, so you dont have to maintain the dataprovider as an attribute:

 

var listOfThings:DropDown = new DropDown();

var arr:Array = new Array([{label:'green'},
	{label:'blue'},
	{label:'orange'},
	{label:'red'},
	{label:'yellow'},
	{label:'silver'}] );

listOfThings.dataProvider = new DataProvider( arr ); 

If an item is not initially selected by the code or the user, selectedItem can be null, so it is good practice to do:

 

var selection : String = listOfThings.selectedItem ? listOfThings.selectedItem.label : '';

 

 It is also good practice to not be the physical label of a menu, since the label the user may see may change (different languages) but the underlying meaning be the same.  So I like to stress the following when possible (separation of data from UI):

 

var listOfThings:DropDown = new DropDown();

var arr:Array = new Array([{data:'green',label:'Green'},
	{data:'blue',label:'Blue'},
	{data:'orange',label:'Orange'},
	{data:'red',label:'Red'},
	{data:'yellow',label:'Yellow'},
	{data:'silver',label:'Silver'}] );

listOfThings.dataProvider = new DataProvider( arr );

 Then:

var selection : String = listOfThings.selectedItem ? listOfThings.selectedItem.data: '';

 

 

Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: DropDown selection

thanks for the update John. you have some good points but i was thinking of doing the line arr[listOfThings.selectedIndex].label inside of a listener function. but your way may be more efficient. ill take it into account.

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
Developer
kaputafj
Posts: 57
Registered: ‎05-04-2009
My Device: 9550

Re: DropDown selection

Thanks both - got it working.

Contributor
durvasa
Posts: 14
Registered: ‎12-18-2010
My Device: Not Specified

Re: DropDown selection

[ Edited ]

Sorry to bring up an old thread, but I've been trying to figure out some of the instructions/answers written above. 

 

I have a drop down list populated from on array.  When the user makes a selection from the drop down list in the GUI of my app, I'm trying to access the value so I can use it for another part of the program.  Here's the code I'm working on -- I cant seem to get it to work

 

 

////
var sizeChoice:DropDown = new DropDown(); sizeChoice.rowHeight = 20; sizeChoice.width = 100; sizeChoice.x = 150; sizeChoice.y = 50; sizeChoice.height = 30; var dp:DataProvider = new DataProvider(); var arr:Array = new Array([{data:'1',label:'1'},{data:'2',label:'2'}, {data:'55',label:'55'}] ); dp.setItems(arr[0]); sizeChoice.dataProvider = dp; sizeChoice.rowCount = dp.length; sizeChoice.addEventListener(Event.SELECT,onClick);

///////

public function onClick(event:Event):void
{
trace(event.currentTarget.selectedIndex);
trace(event.target.selectedIndex);
}

 

 

When I run the app based on the code above and click on a number in the drop down menu, the trace function correctly outputs the selected index.  The problem I have is that I dont want the selected index -- I want the data in the array at that index and I'm not quite sure how to do it.

 

I tried all of these, and I get errors:

1. var url:smileyfrustrated:tring = arr[event.target.selectedIndex].data;

2. trace (event.dp.data);

3. trace (event.dp.label);

 

Thanks for your help!

 

 

Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: DropDown selection

 


durvasa wrote:

Sorry to bring up an old thread, but I've been trying to figure out some of the instructions/answers written above. 

[snip code using selectedIndex]

When I run the app based on the code above and click on a number in the drop down menu, the trace function correctly outputs the selected index.  The problem I have is that I dont want the selected index -- I want the data in the array at that index and I'm not quite sure how to do it.

 


The examples above showed exactly how to do it.  You've been using selectedIndex... look at the examples: they use a different attribute on the DropDown object.

 


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: DropDown selection

hey,

 

yes peter is correct. try using event.target.selectedItem.data instead and see how that works out. 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