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
New Contributor
Posts: 4
Registered: ‎05-24-2012
My Device: Blackberry Torch 9800
My Carrier: Vodafone

Not Able to load Different urls in BrowserField based on drop menu selection

Hi i just started with blackberry development.i am trying to build a simple app having objectchoicefield.Based on value selected in drop list,i tried to load various url's but iam not been able to.I am attaching my code also.Pls reply

 

String choices[] = {"Option","Sales Dashboard","Enterprise Dashboard","Incident Dashboard"};
int iSetTo = 0;
ObjectChoiceField ConversionChoice = new ObjectChoiceField("Select DashBoard",choices,iSetTo);
add(ConversionChoice);
BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
BrowserField browserField = new BrowserField(myBrowserFieldConfig); 
add(browserField);

String selectedValue = choices[ConversionChoice.getSelectedIndex()];

if (selectedValue.equals(choices[1])){
browserField.requestContent("http://www.google.com");
}

else if (selectedValue.equals(choices[2])){
browserField.requestContent("http://www.yahoo.com");
}

else if (selectedValue.equals(choices[3])){
browserField.requestContent("http://www.facebook.com");
}

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Not Able to load Different urls in BrowserField based on drop menu selection

"I tried to load various url's but iam not been able to"

 

For next time, can you be a little more specific about the problem you face.  Also it pays to debug your code so that you understand what is going wrong to.

 

The problem here is that your code assumes that it stops at this point and waits for user input:

 

String selectedValue = choices[ConversionChoice.getSelectedIndex()];

 

It doesn't stop there.  In fact at that point the choice is not even on the screen!

 

You need to create an event handler that detects user input.  This is usually a Field Change Event and you code a FieldChanged listener.  Look round the forum for samples of how to code this for an ObjectChoiceField. 

 

Good luck. 

New Contributor
Posts: 4
Registered: ‎05-24-2012
My Device: Blackberry Torch 9800
My Carrier: Vodafone

Re: Not Able to load Different urls in BrowserField based on drop menu selection

hi thnx for reply..i just started with blackberry development to couldnt got the specific problem.

 

Now i have used a setlistener for objectfield

ConversionChoice.setChangeListener(this);

 

and selected index from list i could get from this function


public void fieldChanged(Field field, int context) {
int selectedIndex = ConversionChoice.getSelectedIndex();
index = selectedIndex;
}

 

index is global variable from which i try to load diff url's.for eg:

 

if (index == 1) {

browserField.requestContent("http://");
browserField.refresh();
}

else if

so on..

 

 

but still no url loads...

 


Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Not Able to load Different urls in BrowserField based on drop menu selection

I suspect you still have a timing issue.  Your change listener sets the index, but what process will kick on the display?

 

Put break points in your code and figure out what order everything happens in.

 

Basically you need to move the code that using 'index' into the listener. 

 

Remember Blackberry is an Event driven OS.  You need to get your head around this to understand how to program it correctly.

 

 

New Contributor
Posts: 4
Registered: ‎05-24-2012
My Device: Blackberry Torch 9800
My Carrier: Vodafone

Re: Not Able to load Different urls in BrowserField based on drop menu selection

Thnx for replying...as u suggested i moved browserfield code in onfieldchange()  and that seems to work .But browserfield loads each url from start to the selected value in loop.

 

if (selectedIndex == 1) {
browserField1.requestContent("http://www.google.com");
 }

else if (selectedIndex == 2) {
browserField1.requestContent("http://www.facebook.com");
}

 

if index is 1,then it loads first url,but if index is 2,it tries to load both 1st and 2nd url.

 

i tried using setfocus() and refresh() on browserfield,but it results in java exception.

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Not Able to load Different urls in BrowserField based on drop menu selection

One of the problem with ChoiceField is that it drives the field Change listner when it is 'going through' a value as well as when it has stopped on a value.  So if the Field is on position 0, it will drive the change listener with position 1 and then position 2 when scrolling to position 2.  You should use the debugger to confirm this is the situation you see.

 

The usual approach to this is to have a choice field (which you have), followed by a 'GO button.  So the user selects his/her choice, then presses the button to go to that page.  You have the change listener on the GO button and not on the choice field, but it will still pick up the index from the choice field.