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
M4Rv
Posts: 9
Registered: ‎08-03-2010
My Device: Bold 9700
My Carrier: o2 Germany
Accepted Solution

Remove Item from Picker

Hey guys,

 

I am currently working on an app where I need to have a function that is able to remove Items from a Picker.

I have an array which includes all the items. And create the Picker similar to this example codes: Link

 

This is my basic approach code (does not work):

 

private function removeSubject(subject:String):void {
	var newArrSubjects:Array = [];
	for(var i:int=0; i<arrSubjects.length; i++){
		if(!(arrSubjects[i].label.equals(subject)))
		newArrSubjects.push({label: arrSubjects[i].label});
	}
	arrSubjects = newArrSubjects;
	subjectPicker.dataProvider = createDataProviderSubject();
}

 

My problem is that I don't know how to really to the test of Equality or how to get the contents of the "label".

 

 

I have a function to add an item to the Picker working like that:

 

 

private function addSubject(subject:String):void {
	arrSubjects.push({label: subject});
	subjectPicker.dataProvider = createDataProviderSubject();		}

 The createDataProviderSubject() uses the arrSubjects as their Source to create the DataProvider.

 

 

Would be very pleased if anyone had a great idea to help me out :smileyhappy:

 

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

Re: Remove Item from Picker

hey,

 

try running the following code instead and see what you get:

 

 

private function removeSubject(subject:String):void
{

    for(var i:int=0; i < arrSubjects.length; i++)
    {
        if(arrSubjects[i].label == subject)
        {
            /**
             * use the splice method of the array
             * to delete the object based on the index
             *
             */
            newArrSubjects.splice(i,1);

            /** if there is a match break out of for loop */
            break;
        }
    }

    subjectPicker.dataProvider = createDataProviderSubject();
}

 

This time we are using the same array and simply removing the object u dont want anymore. hope that clears things up. 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
M4Rv
Posts: 9
Registered: ‎08-03-2010
My Device: Bold 9700
My Carrier: o2 Germany

Re: Remove Item from Picker

wow, thank you so much!

-----------------
Approved Apps: Melody, Alcoholometer
Please use plain text.
New Contributor
M4Rv
Posts: 9
Registered: ‎08-03-2010
My Device: Bold 9700
My Carrier: o2 Germany

Re: Remove Item from Picker

[ Edited ]

Still one issue.

I get an error when trying to delete the last item (the one at arrSubject.length-1) of the Picker / Array.

This is what I get:

 

Bildschirmfoto 2011-03-26 um 15.11.09.png

But the Array / the Picker contains 10 items, so index 9 should be just fine for the last element :/

-----------------
Approved Apps: Melody, Alcoholometer
Please use plain text.
New Contributor
M4Rv
Posts: 9
Registered: ‎08-03-2010
My Device: Bold 9700
My Carrier: o2 Germany

Re: Remove Item from Picker

[ Edited ]

I also rewrote the removeSubject() method.

This time I directly take the index currently selected by the Picker and delete this index in the Array.

 

 

private function removeSubject():void {
	var index:int = subjectPicker.selectedIndices[0];
		
	arrSubjects.splice(index,1);
			
	subjectPicker.dataProvider = createDataProviderSubject();
}

 

To make sure it's not the splice method causing the error I also tried this one:

 

 

private function removeSubject():void {
	var index:int = subjectPicker.selectedIndices[0];
			
	if(index == arrSubjects.length-1){
		arrSubjects.pop();
	} else {
		arrSubjects.splice(index,1);
	}
			
	subjectPicker.dataProvider = createDataProviderSubject();
}

 

But i get the same error message...

 

 

 

-----------------
Approved Apps: Melody, Alcoholometer
Please use plain text.
New Contributor
M4Rv
Posts: 9
Registered: ‎08-03-2010
My Device: Bold 9700
My Carrier: o2 Germany

Re: Remove Item from Picker

Ok, i got it solved.

The selectedIndices of the picker needed to be set to a valid value before deleting the item and creating a new DataProvider.

-----------------
Approved Apps: Melody, Alcoholometer
Please use plain text.