BlackBerryForums.com : Your Number One BlackBerry Community      

»Sponsored Links



Reply
 
LinkBack Thread Tools
  (#1 (permalink)) Old
dynot Offline
Knows Where the Search Button Is
 
Posts: 46
Join Date: Oct 2007
Model: 8100
PIN: N/A
Carrier: rogers
Default How can I use a variable globally? - 06-22-2008, 08:47 PM

I'm stumped! Here's what I'm trying to do.

On the first screen I want to enter 12 different variables. I'm using NumericChoiceField since all variables are less than 20.

At the bottom of the screen I placed a button titled "Calculate" so that when the user clicks it another screen is displayed with results. I'm using the FieldChangeListner to capture the click event and that's where my problem is.

None of the NumericChoiceValues get passed over for the calculations. I'm assuming its because its in a different class. I read somewhere that I can pass these values when the the class is triggered but there was no example on how to do this. I tried to work it out myself but keep getting various errors.

Can anyone give me suggestions?
   
Reply With Quote
Sponsored Links
Please Login or Register to Remove these Advertisements!

  (#2 (permalink)) Old
shraddha294 Offline
Thumbs Must Hurt
 
shraddha294's Avatar
 
Posts: 77
Join Date: Oct 2007
Location: Hyderabad
Model: 8800
PIN: N/A
Carrier: TMobile
Default 06-23-2008, 01:07 AM

What did u try to work out and what errors did u get?

At first glance i feel, if u r using Java, u can make an array as a data member of class containing numeric choice fields and store the values in an array.
If u tried this and got errors, let us know...
   
Reply With Quote
  (#3 (permalink)) Old
simon.hain Offline
CrackBerry Addict
 
Posts: 711
Join Date: Apr 2005
Location: hamburg, germany
Model: 8700
Carrier: o2
Default 06-23-2008, 02:47 AM

Quote:
Originally Posted by dynot View Post
I'm stumped! Here's what I'm trying to do.

On the first screen I want to enter 12 different variables. I'm using NumericChoiceField since all variables are less than 20.

At the bottom of the screen I placed a button titled "Calculate" so that when the user clicks it another screen is displayed with results. I'm using the FieldChangeListner to capture the click event and that's where my problem is.

None of the NumericChoiceValues get passed over for the calculations. I'm assuming its because its in a different class. I read somewhere that I can pass these values when the the class is triggered but there was no example on how to do this. I tried to work it out myself but keep getting various errors.

Can anyone give me suggestions?
the clean way to do something like this is to implement a storage class that is known by both screens, using interfaces.
a more simple solution would be to give the second screen a reference of the first and put some public getValue-methods into the first screen.


java developer, Devinto, hamburg/germany
   
Reply With Quote
  (#4 (permalink)) Old
dynot Offline
Knows Where the Search Button Is
 
Posts: 46
Join Date: Oct 2007
Model: 8100
PIN: N/A
Carrier: rogers
Default 06-23-2008, 09:44 AM

Thanks to both for the suggestions.

What I tried to do is pass the numericchoicefields as a parameter into the fieldchangelistener class. The problem is most likely due to the structure of my code. An example of how your suggestions should look would be of great help.

I've read up a lot on Java in general and BB development however I find there is very little in terms of sample codes. I haven't been able to find anything anywhere on how to pass parameters or even how to declare a variable globally.

Therefore an example of how to make an array a data member of a class or how to implement a storage class that is known by both screens would go a long way. Or for that matter, where can I find documentation on these techniques?

Thanks again...

PS: the RIM site does not have any decent documentation. I've been reading Sun's The Java Tutorial mostly but even that doesn't explain very well.

Last edited by dynot : 06-23-2008 at 09:46 AM.
   
Reply With Quote
  (#5 (permalink)) Old
jan.g Offline
Knows Where the Search Button Is
 
Posts: 21
Join Date: May 2007
Model: 8310
PIN: N/A
Carrier: TMO
Default 06-23-2008, 11:17 AM

Well if I unterstand you correctly, you just want to pass some values from one Screen to another? An easy way to do this is to hand them over in the constructor of your second screen. Your constructor would look like this:

Code:
public SecondScreen(int value1, int value2 .... ) {
// some stuff here
}
and to push the screen:

Code:
UiApplication.getUiApplication().pushScreen(new SecondScreen(choice1.getSelectedValue(), choice2.getSelectedValue() ..... ));
   
Reply With Quote
  (#6 (permalink)) Old
dynot Offline
Knows Where the Search Button Is
 
Posts: 46
Join Date: Oct 2007
Model: 8100
PIN: N/A
Carrier: rogers
Default 06-23-2008, 02:09 PM

Quote:
Originally Posted by jan.g View Post
Well if I unterstand you correctly, you just want to pass some values from one Screen to another? An easy way to do this is to hand them over in the constructor of your second screen. Your constructor would look like this:

Actually, my first screen has a button which the user clicks to calculate the results. I use a FieldChangeListener at which point the control passes to that section so that the calclations could be done and the second screen with the results displayed. Its when the FieldChangeListener is triggered that I lose all the variables.

Maybe I'm not using the right approach?
   
Reply With Quote
  (#7 (permalink)) Old
dynot Offline
Knows Where the Search Button Is
 
Posts: 46
Join Date: Oct 2007
Model: 8100
PIN: N/A
Carrier: rogers
Default 06-23-2008, 07:52 PM

Sorry jan.g. I don't understand.

Maybe posting the code I have may help. I deleted some lines which were repetitive but this is basically how it looks:

package com.rim.samples.device.CPTcost;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.i18n.*;
import net.rim.device.api.system.*;
import net.rim.device.api.collection.util.*;

class CPTcost extends UiApplication
{
public static void main(String[] args)
{
CPTcost theApp = new CPTcost();
theApp.enterEventDispatcher();
}
public CPTcost()
{
// Display a new screen
pushScreen(new CPTcostScreen());
}


class CPTcostScreen extends MainScreen
{
public NumericChoiceField cskins;
public CPTcostScreen()
{
super(DEFAULT_MENU | DEFAULT_CLOSE);
setTitle(new LabelField("CPT Costs", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));
add(new SeparatorField());

CheckboxField chk1 = new CheckboxField("Half Gallons", true);
add(chk1);
add(new SeparatorField());
NumericChoiceField uom = new NumericChoiceField("Unit of measure: ", 0, 12, 1);
add(uom);
NumericChoiceField uc = new NumericChoiceField("Unit cost: ", 15, 50, 5);
add(uc);
NumericChoiceField sc = new NumericChoiceField("Surcharge: ", 1, 10, 1);
add(sc);

ButtonField btn = new ButtonField("Calculate");
btn.setChangeListener(new ButtonListener());
add(btn);
}


class ButtonListener implements FieldChangeListener
{
public void fieldChanged(Field field, int context)
{
ButtonField btn = (ButtonField) field;
Dialog.alert("This is the UOM: " + uom);
}
}


public boolean onClose()
{
// Display a farewell message before closing application.
Dialog.alert("Exiting...");
System.exit(0);
return true;
}

}
}

The CPTcostScreen is where the user inputs the variables. When done, the button field is clicked to execute the calculations. After that is done, a new screen will be displayed with the results. I haven't started writing these last two steps since I can't get by this problem yet but that should be relatively easy to do.

Will the calculations and the results screen coding be done in the ButtonListener class? Critiques are welcomed.

Thanks in advance.
   
Reply With Quote
  (#8 (permalink)) Old
jan.g Offline
Knows Where the Search Button Is
 
Posts: 21
Join Date: May 2007
Model: 8310
PIN: N/A
Carrier: TMO
Default 06-24-2008, 03:30 AM

Ok first, please use [code]-Tags when posting code on the forums, it helps people read it a lot.

But oh well, your problem is, you need to declare your NumericChoiceFields outside the scope of your screen constructor, otherwise they won't be known in your ChangeListener. So basically put "NumericChoiceField uom, sc, us;" somewhere in your class and change "NumericChoiceField uom = new...." to "uom = new...", to avoid them being seen as local variables.
   
Reply With Quote
  (#9 (permalink)) Old
goulamass Offline
Talking BlackBerry Encyclopedia
 
Posts: 204
Join Date: Jan 2008
Location: France
Model: 8310
PIN: N/A
Carrier: Vodafone
Default 06-24-2008, 04:22 AM

First I suggest to use protected boolean trackwheelClick(int status, int time) instead of public void fieldChanged(Field field, int context) for your button.

And there you recover all the value and process them
   
Reply With Quote
  (#10 (permalink)) Old
dynot Offline
Knows Where the Search Button Is
 
Posts: 46
Join Date: Oct 2007
Model: 8100
PIN: N/A
Carrier: rogers
Default 06-25-2008, 09:41 PM

Thanks to all...

I used a second screen and push it unto the stack. I figured out how to declare my variables globally. Now I have another problem but I'll start a new thread.
   
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On






Copyright © 2004-2008 BlackBerryNews.com, BlackBerryFAQ.com, BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of Research In Motion Limited.
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.1