Switch to new screen when one of the ObjectChoiceField is selected in black berry - blackberry

Can any one tell me how to switch to another screen when one of ObjectChoiceField is selected.
Thank You.

final class HelloWorldScreen extends MainScreen implements FieldChangeListener
{
ObjectChoiceField choice=null;
public HelloWorldScreen()
{
super();
String choicestrs[] = {"Opt 1", "Opt 2", "Opt 3"};
choice = new ObjectChoiceField("Object Choice Field: ", choicestrs, 0);
choice.setChangeListener(this);
add(choice);
}
public void openAnotherForm(){
AnotherForm newScreen = new AnotherForm();
UiApplication.getUiApplication().pushScreen(newScreen);
}
public void fieldChanged(Field arg0, int arg1) {
openAnotherForm();
}
}
class AnotherForm extends MainScreen
{
public AnotherForm()
{
super();
add(new LabelField("Another Form"));
}
}

Related

Blackberry delete menu item

I have mainscreen with 5 menus and after a certain action I need to delete 2 of them in a method that peforms the action, can I delete menu items programetically ?
Run this sample code:
public class Abc extends MainScreen
{
ButtonField click;
MenuItem saveMenu;
public Abc()
{
createGUI();
}
private void createGUI()
{
saveMenu=new MenuItem("Save", 100, 101);
addMenuItem(saveMenu);
click=new ButtonField("click");
click.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
removeTheMenu(saveMenu);
}
});
add(click);
}
public void removeTheMenu(MenuItem menuItem)
{
Screen screen=Ui.getUiEngine().getActiveScreen();
Menu menu=screen.getMenu(0);//Gives the Menu list of active screen only;
for(int i=0;i<menu.getSize();i++)
{
if(menu.getItem(i).toString().equalsIgnoreCase(menuItem.toString()))
{
removeMenuItem(menuItem);
Status.show("Removed Successfully", 1000);
}
}
}
}
This code may help you;

Java Blackberry Refresh Field Label

I have a main class and a screen class. I have a button that launches a datepicker. When the datepicker is closed I need the label of the button to be refreshed with the selected value. How should I do that?
I tried with invalidate, creating a CustomButtonField that implements different onFocus, onUnFocus, and some other stuff but I guess I implemented them in a wrong way...
My two clases are these ones...
Main...
public class TestClass extends UiApplication
{
public static Calendar alarm1time = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
public static void main(String[] args)
{
TestClass testClass = new TestClass ();
testClass.enterEventDispatcher();
}
public TestClass()
{
pushScreen(new TestClassScreen());
}
}
Screen...
public final class TestClassScreen extends MainScreen
{
public TestClassScreen()
{
ButtonField alarm1 = new ButtonField("Alarm : " + TestClass.alarm1time.get(Calendar.HOUR_OF_DAY) + ":" + TestClass.alarm1time.get(Calendar.MINUTE) ,ButtonField.FOCUSABLE)
{
public boolean navigationClick (int status , int time)
{
datePicker(1);
return true;
}
};
setTitle("Test Alarm");
add(new RichTextField(" "));
add(alarm1 );
}
public void datePicker(final int alarmNumber)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
DateTimePicker datePicker = null;
datePicker = DateTimePicker.createInstance(TestClass.alarm1time, null, "HH:mm");
if(datePicker.doModal())
{
Calendar cal = datePicker.getDateTime();
TestClass.alarm1time = cal;
//Here I need the label to be refreshed, after the datePicker is Ok
}
}
});
}
}
I found one way in the Blackberry Development Forum...
public boolean navigationClick (int status , int time)
{
datePicker(1, this);
return true;
}
public void datePicker(final int alarmNumber, final ButtonField buttonToUpdate)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
DateTimePicker datePicker = null;
datePicker = DateTimePicker.createInstance(TestClass.alarm1time, null, "HH:mm");
if(datePicker.doModal())
{
Calendar cal = datePicker.getDateTime();
TestClass.alarm1time = cal;
buttonToUpdate.setLabel(....)
}
}
});
}
A more usual way would be to have change listener listen for Button Clicks and do similar processing in there.
I think, this helps you:
public class Def extends MainScreen
{
ButtonField show;
LabelField label;
String str="";
ObjectChoiceField choiceField;
public Def()
{
createGUI();
}
private void createGUI()
{
String st_ar[]={"Date Picker"};
choiceField=new ObjectChoiceField("Select Date: ", st_ar)
{
protected boolean navigationClick(int status, int time)
{
DateTimePicker datePicker = DateTimePicker.createInstance( Calendar.getInstance(), "yyyy-MM-dd", null);
datePicker.doModal();
Calendar calendar=datePicker.getDateTime();
str=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.YEAR);
return true;
}
};
add(choiceField);
label=new LabelField("",Field.NON_FOCUSABLE);
add(label);
show=new ButtonField("Show");
show.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
label.setText("Time is: "+str);
}
});
add(show);
}
}

Get field value from PopupScreen back to MainScreen in Blackberry

Now i have a MyScreen which extends from MainScreen containing a button. When the button clicked, it will show the PopupScreen containing list of RadioButtonFields, OK and Cancel buttons. My question is how to get RadioButtonField value from PopupScreen back to MyScreen.
Please help me in this case.
Thanks
Below is my code
MyScreen.java
public class MyScreen extends MainScreen{
private int currentValue = 0;
public MyScreen() {
LabelField lblField = new LabelField();
lblField.setText("Current Value = " + currentValue);
ButtonField btnPopup = new ButtonField("Show Popup");
btnPopup.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(new MyPopupScreen());
}
});
add(lblField);
add(btnPopup);
}
}
MyPopupScreen
public class MyPopupScreen extends PopupScreen implements FieldChangeListener {
private int currentValue = -1;
public MyPopupScreen() {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR), Field.FOCUSABLE
);
add(new LabelField("Select Chart Type"));
RadioButtonGroup radioGroup = new RadioButtonGroup();
for (int i = 0; i < 10; i++) {
RadioButtonField radio = new RadioButtonField("Option no. " + i, radioGroup, false);
radio.setChangeListener(this);
add(radio);
}
ButtonField cancelBtn = new ButtonField("Close");
ButtonField okBtn = new ButtonField("OK");
add(okBtn);
add(cancelBtn);
cancelBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().popScreen();
}
});
okBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
//How to get radio value or index value to pass back to MyScreen
}
});
}
public void fieldChanged(Field field, int context) {
if (field instanceof RadioButtonField) {
RadioButtonField radio = (RadioButtonField) field;
currentValue = radio.getIndex();
}
}
}
As Vijay, suggested this is one way that we could achieve this using inner class. The other approach could be, using design observer design patterns.
Try understanding the below code, and use what ever is efficient for you.
MyScreen.java
public class MyScreen extends MainScreen implements MyListener {
MyScreen screen = null;
public MyScreen() {
screen = this;
LabelField lblField = new LabelField();
lblField.setText("Current Value = " + currentValue);
ButtonField btnPopup = new ButtonField("Show Popup");
btnPopup.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(new MyPopupScreen(screen));
}
});
add(lblField);
add(btnPopup);
}
public void valueChanged(int selectedIdx) {
Dialog.alert(selectedIdx );
}
}
MyListener.java
public interface MyListener {
public void valueChanged(int selectedIdx);
}
MyPopupScreen.java
public class MyPopupScreen extends PopupScreen implements FieldChangeListener {
MyScreen deligate = null;
public MyPopupScreen(final MyScreen deligate) {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR), Field.FOCUSABLE);
this.deligate = deligate;
add(new LabelField("Select Type"));
ButtonField closeBtn = new ButtonField("Close");
ButtonField okBtn = new ButtonField("OK");
add(okBtn);
add(closeBtn);
closeBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
});
okBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
deligate.valueChanged(radioGroup.getSelectedIndex());
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
});
}
}
public class MyScreen extends MainScreen{
private int currentValue = 0;
String result; // Declare the result
public MyScreen() {
LabelField lblField = new LabelField();
lblField.setText("Current Value = " + currentValue);
ButtonField btnPopup = new ButtonField("Show Popup");
btnPopup.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(new MyPopupScreen());
}
});
ButtonField btnGetResult = new ButtonField("result"); //Add new Button
btnGetResult.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.inform(result);
}
});
add(lblField);
add(btnPopup);
add(btnGetResult);// Add button to get the selected index
}
public class MyPopupScreen extends PopupScreen implements FieldChangeListener {
private int currentValue = -1;
RadioButtonField radio;
public MyPopupScreen() {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR), Field.FOCUSABLE
);
add(new LabelField("Select Chart Type"));
final RadioButtonGroup radioGroup = new RadioButtonGroup();
for (int i = 0; i < 10; i++) {
radio = new RadioButtonField("Option no. " + i, radioGroup, false);
radio.setChangeListener(this);
add(radio);
}
ButtonField cancelBtn = new ButtonField("Close");
ButtonField okBtn = new ButtonField("OK");
add(okBtn);
add(cancelBtn);
cancelBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().popScreen(MyPopupScreen.this);
}
});
okBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
result = radioGroup.getSelectedIndex()+"";
UiApplication.getUiApplication().popScreen(MyPopupScreen.this);
}
});
}
public void fieldChanged(Field field, int context) {
if (field instanceof RadioButtonField) {
RadioButtonField radio = (RadioButtonField) field;
currentValue = radio.getIndex();
}
}
}
}

How to configure your blackberry app works ok (works on simulator but not on smartphone)?

I wrote an app which has 2 screens. The first screen is triggered by the main class. The second screen is opened by clicking a button in the first screen.
public class MyApp extends UiApplication{
public static void main(String[] args){
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
public MyApp(){
// Push a screen onto the UI stack for rendering.
pushScreen(new MyScreen());
}
}
public class MyScreen extends MainScreen implements FieldChangeListener
{
BasicEditField mEdit = null;
ButtonField mButton = null;
public MyScreen()
{
super();
mEdit = new BasicEditField("input: ", "some text");
add(mEdit);
mButton = new ButtonField("Go second screen");
mButton.setChangeListener(this);
add(mButton);
}
public void fieldChanged(Field field, int context)
{
if(mButton == field)
{
MyScreen2 scr = new MyScreen2();
scr.setTextValue(mEdit.getText());
UiApplication.getUiApplication().pushScreen(scr);
UiApplication.getUiApplication().popScreen(this);
}
}
}
public final class MyScreen2 extends MainScreen
{
String mTextValue = null;
LabelField mLabel = null;
public void setTextValue(String textValue)
{
mTextValue = textValue;
mLabel.setText(mTextValue);
}
public MyScreen2()
{
super();
mLabel = new LabelField();
add(mLabel);
}
}
It works on the 9700 simulator, but doesn't work on the smartphone. I wonder what is wrong? I wonder if the smartphone blocks loading app from my computer?
I tried signing .cod but nothing changes.
Any idea?
you need signing key to run your application on real device ... It cost near about 20 dollars
go here
you can find all the details from here
I think it might help you
cheers

Blackberry: select item in a list, return to previous screen

I have prepared a very short test case (s. below) for my question.
On a button click I would like to display a list of strings in a new screen.
After the user selects one item in the list, the previous screen should be displayed again and the button label should be set to the selected string.
My 2 problems are:
From inside the menu I don't know how to pop the currently displayed screen
How to pass the selected item from one screen to another (assuming I don't want to introduce a public variable/method on the former screen as a workaround)
Please suggest the necessary changes for my src\mypackage\MyList.java:
package mypackage;
import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.system.*;
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.ui.decor.*;
import net.rim.device.api.util.*;
import net.rim.device.internal.i18n.*;
public class MyList extends UiApplication implements FieldChangeListener {
MyScreen myScreen = new MyScreen();
public static void main(String args[]) {
MyList app = new MyList();
app.enterEventDispatcher();
}
public MyList() {
MainScreen titleScreen = new MainScreen();
titleScreen.setTitle("Click the button:");
// TODO change the label of this button (see below)
ButtonField myButton = new ButtonField("Show the list", ButtonField.CONSUME_CLICK);
myButton.setChangeListener(this);
titleScreen.add(myButton);
pushScreen(titleScreen);
}
public void fieldChanged(Field field, int context) {
pushScreen(myScreen);
}
}
class MyScreen extends MainScreen {
ObjectListField myList = new ObjectListField();
public MyScreen() {
setTitle("Select an item below:");
myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", });
add(myList);
addMenuItem(myMenu);
}
private final MenuItem myMenu = new MenuItem("Select item", 0, 0) {
public void run() {
int index = myList.getSelectedIndex();
if (index < 0)
return;
String item = (String) myList.get(myList, index);
Status.show("Selected: " + item);
// TODO how to return to the previous screen here?
// TODO how to call myButton.setLabel(item) here?
}
};
}
Thank you!
Alex
Use a callback pattern:
class TitleScreen extends MainScreen {
private ButtonField myButton;
public TitleScreen() {
super();
setTitle("Click the button:");
// TODO change the label of this button (see below)
myButton = new ButtonField("Show the list", ButtonField.CONSUME_CLICK);
myButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
OnItemSelectedCallback callback =
new OnItemSelectedCallback() {
public void onItemSelected(String label) {
TitleScreen.this.onItemSelected(label);
}
};
MyScreen myScreen = new MyScreen(callback);
UiApplication.getUiApplication().pushScreen(myScreen);
}
});
add(myButton);
}
private void onItemSelected(String label) {
// this will be called when a menu item is executed on the MyScreen
// e.g. you can call smth like: myButton.setLabel(label);
}
}
interface OnItemSelectedCallback {
void onItemSelected(String label);
}
class MyScreen extends MainScreen {
ObjectListField myList = new ObjectListField();
private final OnItemSelectedCallback onItemSelectedCallback;
public MyScreen(OnItemSelectedCallback onItemSelectedCallback) {
setTitle("Select an item below:");
this.onItemSelectedCallback = onItemSelectedCallback;
myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", });
add(myList);
addMenuItem(myMenu);
}
private final MenuItem myMenu = new MenuItem("Select item", 0, 0) {
public void run() {
int index = myList.getSelectedIndex();
if (index < 0)
return;
String item = (String) myList.get(myList, index);
Status.show("Selected: " + item);
// TODO how to return to the previous screen here?
// TODO how to call myButton.setLabel(item) here?
// notify the parent screen
onItemSelectedCallback.onItemSelected(item);
// close the current screen
UiApplication.getUiApplication().popScreen(MyScreen.this);
}
};
}

Resources