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 Developer
david_schwartz_bb
Posts: 137
Registered: ‎02-04-2010
My Device: Tour 9630
Accepted Solution

Sorting Lists

Can a sort order be applied to a qnx.ui.data.DataProvider? Want to sort a list of names alphabetically. Thanks
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Sorting Lists

hey david,

 

there doesnt seem to be a direct way to do the sorting. you would have to create your own custom function to sort and refresh the list. (although if you change items on a dataprovider, it automatically refreshes the lsit for you). My theory is if you can pull all the data out of the dataprovider and store it into a seperate array and then sort that enw array and after that "reset" the items on your dataprovider with the new array that is sorted using the setItems() method on your dataprovider. ill try to get a working code to show you an example. 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
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Sorting Lists

[ Edited ]

You can do it on the array that gets passed to the data provider.

 

var ar : Array = [ { label : 'bob', id:1234 }, { label : 'fred', id: 4567 } ];

ar.sortOn( 'label' );

mylist.dataProvider = new DataProvider( ar );

 

If they are numeric values, you can add additional parameters to the sortOn method as well as sort direction.  See more at:

 

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html

 

You can probably do this directly to the data provider and then refresh it to update the list.  Worse case, reset the data provider with a new one with the resorted list.

 

 

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

Re: Sorting Lists

Just a follow up, in case anyone is wondering how to sort an existing list, here is a sample code on how to do it on the fly using jtegen's suggestion of the sortOn method:

 

ListTests.as:

 

 

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.data.DataProvider;
	import qnx.ui.listClasses.List;
	
	
	[SWF(width="1024",height="600",backgroundColor="#E8E8E8",frameRate="30")]
	public class ListTests extends Sprite
	{
		private var myList:List;
		private var myDataProvider:DataProvider;
		private var myArray:Array;
		private var myBtn:LabelButton;
		
		public function ListTests()
		{
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			myList = new List();
			myArray = new Array();
			
			myArray.push({label: "Thomas Luddington"});
			myArray.push({label: "Master Maruyn"});
			myArray.push({label: "Master Gardyner"});
			myArray.push({label: "Captain Vaughan"});
			myArray.push({label: "Master Kendall"});
			myArray.push({label: "Master Prideox"});
			myArray.push({label: "Robert Holecroft"});
			myArray.push({label: "Rise Courtenay"});
			myArray.push({label: "Master Hugh Rogers"});
			myArray.push({label: "Thomas Foxe"});
			myArray.push({label: "Edward Nugen"});
			myArray.push({label: "Darby Glande"});
			myArray.push({label: "Edward Kelle"});
			myArray.push({label: "Iohn Gostigo"});
			myArray.push({label: "Erasmus Clefs"});
			myArray.push({label: "Edward Ketcheman"});
			myArray.push({label: "Iohn Linsey"});
			myArray.push({label: "Thomas Rottenbury"});
			myArray.push({label: "Roger Deane"});
			myArray.push({label: "Iohn Harris"});
			
			myDataProvider = new DataProvider(myArray);
			
			myList.dataProvider = myDataProvider;
			
			myList.setPosition(10,10);
			myList.setSize(300,500);
			
			addChild(myList);
			
			myBtn = new LabelButton();
			myBtn.label = "Sort List";
			myBtn.setSize(250, 50);
			myBtn.setPosition(360, 70);
			
			myBtn.addEventListener(MouseEvent.CLICK, sortList);
			
			addChild(myBtn);
			
		}
		public function sortList(e:MouseEvent):void
		{
			/*
			 * Using the existing data in your array we are going
			 * to sort it alphabetically based on the label sort key
			 * using the newly updated array we "reset" the dataprovider
			 * using the setItems method and we throw a change so the list
			 * is updated for us
			*/
			myList.dataProvider.data.sortOn("label");
			myList.dataProvider.setItems(myList.dataProvider.data, true);
		}
	}
}

 

Hope that helps someone. 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
Developer
jffurlan
Posts: 1,003
Registered: ‎01-16-2011
My Device: PlayBook (sim)

Re: Sorting Lists

Don't want to start a new thread for a simple question:

 

If I wanted to list a label and an image in an array, would it look like this:

 

 

myArray = new Array();

myArray.push({ label:"item1", image:"image1.png"} )
myArray.push({ label:"item2", image:"image2.png"} )

 

 

- If you like my response/post, or it helped you find an answer you were looking for, please provide a Kudo - white star to the bottom right of this post. -
- Please use the search bar at the top, or check out the PlayBook FAQ's for help getting started -
- Hockey DrillBook -
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Sorting Lists

myArray.sortOn( 'label' );

Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Sorting Lists

I may have responded to the subject and not the question.

 

To show the image, it will depend on how you implement your custom renderer/skin.  In your example, you will need to set the Image in the renderer to the "image" attribute.  Be careful to have the full URI so that Image can load it properly.

 

There are many good threads on creating a custom renderer.

Developer
jffurlan
Posts: 1,003
Registered: ‎01-16-2011
My Device: PlayBook (sim)

Re: Sorting Lists

Cool, thanks.

 

Now, what if two items were pulling data from the array:

A list gets its data from the label, and and image gets its data from image. Is that possible? And I'm guessing I'd just use custom renderer on the Image attribute?

- If you like my response/post, or it helped you find an answer you were looking for, please provide a Kudo - white star to the bottom right of this post. -
- Please use the search bar at the top, or check out the PlayBook FAQ's for help getting started -
- Hockey DrillBook -
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Sorting Lists

The custom renderer will have an Image as a member variable/attribute.  Create it in the constructor, add it as a child in onAdded, and remove it as a child in onRemoved.  Set it in drawLabel() and position it either in drawLabel() or the constructor.  Make certain the data.image attribute exists or it will throw an exception.

 

Take a look at the many renderer examples and if it is still not clear, re-post your renderer code for those that might be able to help.

Developer
jffurlan
Posts: 1,003
Registered: ‎01-16-2011
My Device: PlayBook (sim)

Re: Sorting Lists

This may sound stupid but are Custom Renderer and Custom Cell Renderer different?

Because I'm not trying to put the image in a list. I want the image to show up else where.

 

Here's an idea of the flow of user interaction:

My first list shows a broad range of topics. When a user selects an item from this list, it loads a secondary list. When a user selects an item from this list, it loads an image on the top half of the screen.

 

I have the first  interaction down. It's getting images to be tied to items in the second list that is driving me bonkers (and getting them to change based on what item is selected).

- If you like my response/post, or it helped you find an answer you were looking for, please provide a Kudo - white star to the bottom right of this post. -
- Please use the search bar at the top, or check out the PlayBook FAQ's for help getting started -
- Hockey DrillBook -