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

Java Development

Reply
Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified
Accepted Solution

Drop down redirect?

I code a lot of javascript, html, php etc but for some reason I can't get this java stuff down =/. Anyways is this possible?

 

I want my app to contain a drop down when a user selects an item in the drop down I want a website to be launched based off of that item.  I can do this in javascript in about 2 min but I can't even get a drop down to display on my simulator haha.

 

 

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: Drop down redirect?

To make drop-down lists in your Blackberry application use ObjectChoiceField class.

 

And add instance of this class to the screen.

Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: Drop down redirect?


tbilisoft wrote:

To make drop-down lists in your Blackberry application use ObjectChoiceField class.

 

And add instance of this class to the screen.


I'm using the walkthrough guides as a starting point but they keep giving me an error. This is what my code looks like

 

 

package sgTest; import java.lang.String; import net.rim.device.api.ui.component.ObjectChoiceField; import net.rim.device.api.ui.container.MainScreen; class Test extends MainScreen { Test(){ String choiceItems[] = {"Option 1", "Option 2", "Option 3"}; mainScreen.add(new ObjectChoiceField("Select a category:", choiceItems)); } }

 

 I'm sure it's a stupid error on my part but like I said I'm still learning java.

 

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: Drop down redirect?

should be 

 

String[] choiceItems = new String[] {"choice1", "choice2", "choice3"};

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: Drop down redirect?

Also I do not recommend to include ObjectChoiceField constructor inside call.

 

You need the reference to ObjectChoiceField instance to register FieldChangeListener with this field.

 

It is necessary if you want to intercept field change and update data model according to this field value.

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: Drop down redirect?

Also if you're a novice programmer the following links will be helpful:

 


Also I recommend to download a very useful book. "Thinking in Java" by Bruce Eckel.

In case you have questions - feel free to ask.

 

And good luck Smiley Happy

 

Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: Drop down redirect?

Yeah I've been readingthe developer docs for weeks now and they still are no help. I learn better by just breaking down source code and seeing how things work rather then reading. There is a lack of samples on the net though. Would you mind writing a quick snippit of code that shows how to do it?

 

P.S. The more I use java the more it reminds of c++. I haven't coded in c++ in years but it's slowly coming back to me.

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: Drop down redirect?

[ Edited ]

import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.ObjectChoiceField;

 

 

class MyScreen extends MainScreen implements FieldChangeListener{

private String[] Choices myChoices = new String[] {"Category1", "Category2", "Category3"};

private ObjectChoiceField categorySelectionField = new ObjectChoiceField("Category:", myChoices);

public MyScreen() {

super(.... params ....); // necessary call to parent class constructor

 

// this class implements FieldChangeListener
categorySelectionField.setChangeListener(this);
this.add(categorySelectionField);
}

// FieldChhangeListener interface method
public void fieldChanged(Field field, int context) {
if (field==categorySelectionField) {
int selectedIndex = categorySelectionField.getSelectedIndex();
System.out.println("index selected: " + String.valueOf(selectedIndex));
System.out.println("value selected: " + myChoices[selectedIndex]);
}
}
}

This code creates ObjectChoice field, set the screen as a FieldChanged event listener to intercept situations when user changes field value. And prints in System.out selected value and selected index.
In JDE to view System.out console select from menu: View-Output
Message Edited by tbilisoft on 21-12-2008 06:29 AM
Developer
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: Drop down redirect?

[ Edited ]
Thanks tbilisoft for all your help but it gives me this error

 

private String[] Choices myChoices

 

The jde said there is a problem between Chocies and myChoices a ; was expected

 

 

 

 

There is a ^ sign pointing to the m in myChoices

 

Any ideas?

Message Edited by dmglakewood on 12-20-2008 07:13 PM
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Drop down redirect?

There is no type "Choices" defined, right?

 

Code should read:

 

private String[] myChoices = {"Choice 1", "Choice 2", "Choice 3"};

 

 

 

This creates an array of the three strings.