I want to make my application Blackberry in different languages. That mean I use english as default language and then when the user select other language, all the items and all the application will be with the other language. I use this code and I put the file Local.rrc and .rrh in the same package. I obtain nothing in my screen just white screen. Can any one help me ?
package mypackage;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.i18n.*;
public class Local extends UiApplication {
public static void main(String[] args) {
Local theApp = new Local();
theApp.enterEventDispatcher();
}
public Local() {
pushScreen(new LocalScreen());
}
}
final class LocalScreen extends MainScreen implements LocalDemoResource {
private static ResourceBundle res =
ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);
LabelField title;
RichTextField rtf;
public LocalScreen() {
super();
title = new LabelField(res.getString(FIELD_TITLE),LabelField.ELLIPSIS| LabelField.USE_ALL_WIDTH);
setTitle(title);
rtf = new RichTextField(res.getString(MESSAGE));
add(rtf); }
public void HelloWorldScreen()
{
LabelField title = new LabelField("HelloWorld Sample",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Hello World!"));
}
public boolean onClose() {
Dialog.alert(res.getString(GOODBYE));
System.exit(0);
return true;
}
protected void makeMenu(Menu menu, int instance) {
menu.add(_english);
menu.add(_french);
menu.add(_spanish);
menu.add(_close);
}
private MenuItem _close = new MenuItem(res.getString(CLOSE), 110, 10) {
public void run() {
onClose();
}
};
private MenuItem _english = new MenuItem(res.getString(ENGLISH), 110, 10)
{
public void run() {
Locale.setDefault (Locale.get(Locale.LOCALE_en, null));
refresh();
}
};
private MenuItem _french = new MenuItem(res.getString(FRENCH), 110, 10) {
public void run() {
Locale.setDefault (Locale.get(Locale.LOCALE_fr, null));
refresh();
}
};
private MenuItem _spanish = new MenuItem(res.getString(SPANISH), 110, 10)
{
public void run() {
Locale.setDefault (Locale.get(Locale.LOCALE_es, null));
refresh();
}
};
private void refresh() {
title.setText(res.getString(FIELD_TITLE));
deleteAll();
rtf = new RichTextField(res.getString(MESSAGE));
add(rtf);
_english.setText(res.getString(ENGLISH));
_french.setText(res.getString(FRENCH));
_spanish.setText(res.getString(SPANISH));
_close.setText(res.getString(CLOSE));
}
}
There is an example import--->import blackberry samples---->LocalizationDemo explain how to use multilang in application blackberry
Related
I'm using trying to use a javafx combobox with a cell factory to render the list, I was using the setText on the override updateItem() of my CellList, but I found out when I change a value on the underlying model, that doesnẗ afects the deployed list on the combo box. So I try to make it binding both properties and It's works but when a try to clear the selection I have a Exception. This is the code:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.util.Callback;
public class BasicComboBoxSample extends Application {
public static void main(String[] args) { launch(args); }
#Override public void start(Stage stage) {
final Employee john = new Employee("John");
final Employee jill = new Employee("Jill");
final Employee jack = new Employee("Jack");
final ComboBox<Employee> cboEmployees = new ComboBox();
cboEmployees.getItems().addAll(john, jill, jack);
cboEmployees.setValue(jill);
Button b = new Button("ChangeName");
b.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
john.setName("Maria");
}
});
Button c = new Button("Clear");
c.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
cboEmployees.getSelectionModel().clearSelection();
}
});
Button d = new Button("Select First");
d.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
cboEmployees.getSelectionModel().select(john);
}
});
Callback<ListView<Employee>, ListCell<Employee>> cellFactory = new Callback<ListView<Employee>, ListCell<Employee>>() {
#Override
public ListCell<Employee> call(ListView<Employee> listView) {
return new EmployeeListCell(); //To change body of implemented methods use File | Settings | File Templates.
}
};
cboEmployees.setButtonCell(new EmployeeListCell());
cboEmployees.setCellFactory(cellFactory);
final StackPane layout = new StackPane();
VBox v = new VBox();
v.getChildren().add(cboEmployees);
v.getChildren().add(b);
v.getChildren().add(c);
v.getChildren().add(d);
layout.getChildren().add(v);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 15;");
stage.setScene(new Scene(layout));
stage.show();
}
class Employee {
public Employee(String name) { this.setName(name); }
private SimpleStringProperty name = new SimpleStringProperty("");
String getName() {
return name.get();
}
SimpleStringProperty nameProperty() {
return name;
}
void setName(String name) {
this.name.set(name);
}
}
public class EmployeeListCell extends ListCell<Employee> {
#Override
protected void updateItem(Employee emp, boolean b) {
super.updateItem(emp, b);
if(emp != null){
textProperty().bind(emp.nameProperty());
}
}
}
}
And the Exception was:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: A bound value cannot be set.
at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:157)
at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:67)
at javafx.beans.property.StringProperty.setValue(StringProperty.java:84)
at javafx.scene.control.Labeled.setText(Labeled.java:135)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.updateDisplayText(ComboBoxListViewSkin.java:420)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.updateDisplayNode(ComboBoxListViewSkin.java:399)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.getDisplayNode(ComboBoxListViewSkin.java:229)
at com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.updateDisplayArea(ComboBoxBaseSkin.java:125)
at com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.handleControlPropertyChanged(ComboBoxBaseSkin.java:120)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.handleControlPropertyChanged(ComboBoxListViewSkin.java:198)
at com.sun.javafx.scene.control.skin.SkinBase$3.changed(SkinBase.java:282)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:107)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:367)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:123)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:130)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:148)
at javafx.scene.control.ComboBox.updateValue(ComboBox.java:416)
at javafx.scene.control.ComboBox.access$300(ComboBox.java:166)
at javafx.scene.control.ComboBox$6.changed(ComboBox.java:401)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:367)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:195)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:161)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:130)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:101)
at javafx.scene.control.ComboBox$ComboBoxSelectionModel$1.invalidated(ComboBox.java:448)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:155)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
at javafx.beans.property.ReadOnlyIntegerWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:195)
at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:161)
at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:130)
at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:163)
at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:67)
at javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:208)
at javafx.scene.control.SingleSelectionModel.clearSelection(SingleSelectionModel.java:67)
at de.thomasbolz.javafx.BasicComboBoxSample$2.handle(BasicComboBoxSample.java:45)
at de.thomasbolz.javafx.BasicComboBoxSample$2.handle(BasicComboBoxSample.java:42)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6863)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3328)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
at java.lang.Thread.run(Thread.java:722)
You need to unbind the textProperty in case the emp is null
Add the condition in EmployeeListCell class
else {
textProperty().unbind();
}
Eclipse keeps telling me about invoke problems. This is the message i get as can be seen below. Please help me resolve this problem. What code should i put to get rid of the invoke problem. thanks.
Warning!: method 'parsepack.xmlparsing.navigationClick(int,int)' not invoked.
Warning!: method 'parsepack.xmlparsing.insert(String,int)' not invoked.
here is the method navigationClick()
protected boolean navigationClick(int status, int time)
{
try
{
//navigate here to another screen
ui.pushScreen(new ResultScreen());
}
catch(Exception e)
{
System.out.println("Exception:- : navigationClick() "+e.toString());
}
return true;
}
here is the method insert()
public void insert(String toInsert, int index)
{
listElements.addElement(toInsert);
}
here is the class xmlparsing.java
package parsepack;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class xmlparsing extends UiApplication implements ListFieldCallback, FieldChangeListener
{
public static void main(String[] args)
{
xmlparsing app = new xmlparsing();
app.enterEventDispatcher();
}
public long mycolor ;
Connection _connectionthread;
private static ListField _list;
private static Vector listElements = new Vector();
public MainScreen screen = new MainScreen();
VerticalFieldManager mainManager;
VerticalFieldManager subManager;
UiApplication ui = UiApplication.getUiApplication();
public xmlparsing()
{
super();
pushScreen(screen);
final Bitmap backgroundBitmap = Bitmap.getBitmapResource("blackbackground.png");
mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
public void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, Display.getWidth(),Display.getHeight(),backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
subManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR )
{
protected void sublayout( int maxWidth, int maxHeight )
{
int displayWidth = Display.getWidth();
int displayHeight = Display.getHeight();
super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};
screen.add(mainManager);
_list = new ListField()
{
public void paint(Graphics graphics)
{
graphics.setColor((int) mycolor);
super.paint(graphics);
}
};
mycolor = 0x00FFFFFF;
_list.invalidate();
_list.setEmptyString("* Feeds Not Available *", DrawStyle.HCENTER);
_list.setRowHeight(50);
_list.setCallback(this);
mainManager.add(subManager);
listElements.removeAllElements();
_connectionthread = new Connection();
_connectionthread.start();
}
protected boolean navigationClick(int status, int time)
{
try
{
//navigate here to another screen
ui.pushScreen(new ResultScreen());
}
catch(Exception e)
{
System.out.println("Exception:- : navigationClick() "+e.toString());
}
return true;
}
private class Connection extends Thread
{
public Connection()
{
super();
}
public void run() {
Document doc;
StreamConnection conn = null;
InputStream is = null;
try {
conn = (StreamConnection) Connector.open("http://imforchange.org/international-movement-for-change/testing/data.xml"+";deviceside=true");
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
docBuilderFactory.setCoalescing(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
is = conn.openInputStream();
doc = docBuilder.parse(is);
doc.getDocumentElement().normalize();
NodeList list = doc.getElementsByTagName("eventName");
for (int i = 0; i < list.getLength(); i++)
{
Node textNode = list.item(i).getFirstChild();
listElements.addElement(textNode.getNodeValue());
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ignored) {
}
}
if (conn != null) {
try {
conn.close();
}
catch (IOException ignored) {
}
}
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
_list.setSize(listElements.size());
subManager.add(_list);
screen.invalidate();
}
});
}
}
public void drawListRow(ListField list, Graphics g, int index, int y, int w)
{
String tes = (String)listElements.elementAt(index);
int yPos = 0+y;
g.drawLine(0, yPos, w, yPos);
g.drawText(tes, 5, 15+y, 0, w);
}
public Object get(ListField list, int index)
{
return listElements.elementAt(index);
}
public int indexOfList(ListField list, String prefix, int string)
{
return listElements.indexOf(prefix, string);
}
public int getPreferredWidth(ListField list)
{
return Display.getWidth();
}
public void insert(String toInsert, int index) {
listElements.addElement(toInsert);
}
public void fieldChanged(Field field, int context) {
}
}
ResultScreen.java
package parsepack;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class ResultScreen extends MainScreen {
public ResultScreen() {
LabelField resultLabel = new LabelField("Result: ");
add(resultLabel);
}
}
First of all, those messages you're seeing are warnings, not errors. Warnings are not always a problem, but sometimes they are. In this case, I think that at least the first message is a problem, so you'll want to fix it.
1) First, the navigationClick() method. The compiler is telling you that you have an implementation for the method navigationClick() that is never called from any of your code, or by the BlackBerry OS infrastructure. That's probably not what you want. Normally, navigationClick() is a method that you override in a class you write that extends one of the BlackBerry Field classes. For example, a ButtonField subclass. navigationClick() will be called, in that case, when the button is clicked.
But, you placed your navigationClick() method in the main UIApplication subclass. That's not what you want. You need that method to override the navigationClick() method in a field class. I'm not sure which field you want to have call this method when the user clicks. But, for example, you might do something like this:
_list = new ListField()
{
public void paint(Graphics graphics)
{
graphics.setColor((int) mycolor);
super.paint(graphics);
}
protected boolean navigationClick(int status, int time)
{
try
{
//navigate here to another screen
ui.pushScreen(new ResultScreen());
}
catch(Exception e)
{
System.out.println("Exception:- : navigationClick() "+e.toString());
}
return true;
}
};
That would make navigationClick() get called when your list is clicked, and it will get rid of the warning.
2) Regarding the warning about insert(), that's just because you're not using it anywhere. It look like you've added that method to allow code outside the xmlparsing class to be able to insert items into the list. Maybe that's what you want, but you just haven't yet written the other code to use that method. I see you having at least a few choices:
remove or comment out the insert() method until you need it. this will get rid of the warning.
add some more code that does use this method.
ignore the warning, just making sure you understand that the warning is telling you that you have some code that is not (yet) necessary, since it's unused
you can suppress warnings in Java programs, by doing this, or this, in the Eclipse preferences, if you know the warning is not a problem, and you don't want excessive warnings to hide real problems. I normally don't recommend this. Usually, it's better to fix the warning, than to hide it.
I am using finish() to close current activity before quit application in Android.
However, I cannot close screen in blackberry.
public class Main_AllLatestNews extends MainScreen {
public Main_AllLatestNews() {
super(USE_ALL_WIDTH);
}
private boolean Dialog() {
final Bitmap logo = Bitmap.getBitmapResource("icon.png");
d = new Dialog("确定离开?", new String[] { "是", "否" }, new int[] {
Dialog.OK, Dialog.CANCEL }, Dialog.OK,
logo) {
public void setChangeListener(FieldChangeListener listener) {
if (d.getSelectedValue() == Dialog.OK) {
} else {
d.close();
}
};
};
d.show();
return (d.doModal() == Dialog.OK);
}
public boolean onClose(){
if(Dialog()){
System.exit(0);
return true;
}else
return false;
}
}
Here is my Main class
public class Main extends UiApplication {
public static void main(String[] args) {
Main theApp = new Main();
theApp.enterEventDispatcher();
}
public Main() {
pushScreen(new MyScreen());
}
public final class MyScreen extends MainScreen {
private Bitmap logo = Bitmap.getBitmapResource("logo_page.png");
private BitmapField bmfield;
public MyScreen() {
setTitle("Oriental Daily");
bmfield = new BitmapField(logo, Field.FIELD_HCENTER
| BitmapField.FOCUSABLE) {
protected boolean navigationClick(int status, int time) {
Main.this.pushScreen(new Main_AllLatestNews());
Main.this.popScreen(MyScreen.this);
return true;
}
};
}
}
It depends on exactly how you want your close behaviour to work. Also, I can only read English, so I'm not 100% sure what your Dialog says. I'm assuming it's something to do with closing the app (yes or no)?
Anyway, usually, my apps close by overriding the onClose() method in the MainScreen subclass. You don't actually need to listen for the escape key. onClose() will get called normally when the user escapes all the way out of the app, or presses the little button with the blackberry icon, and then selects Close.
public final class MyScreen extends MainScreen {
/** #return true if the user chooses to close the app */
private boolean showDialog() {
Bitmap logo = Bitmap.getBitmapResource("icon.png");
Dialog d = new Dialog("确定离开?",
new String[] { "是", "否" },
new int[] { Dialog.OK, Dialog.CANCEL },
Dialog.OK,
logo);
return (d.doModal() == Dialog.OK);
}
/** Shutdown the app? */
public boolean onClose() {
if (showDialog()) {
System.exit(0);
return true;
} else {
// the user does not want to exit yet
return false;
}
}
}
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);
}
}
how to make a call from menu item appended in native book of BB('Call from ABC' option)?
Initiate call programmatically
For RIM OS 4.7 and lower use Invoke:
PhoneArguments phoneArgs = new PhoneArguments(PhoneArguments.ARG_CALL,
"555-5555");
Invoke.invokeApplication(Invoke.APP_TYPE_PHONE, phoneArgs);
For RIM OS 5.0 declared we can use Phone.initiateCall method:
Phone.initiateCall(Phone.getLineIds()[0], "519-555-0100");
See Make a call from a BlackBerry device application (multi-line environment)
Add custom menu item to BlackBerry application
To add your "Call via ABC" item to address book menu we need to do following:
implement custom item as an extension to ApplicationMenuItem
add instance of custom item to menu using ApplicationMenuItemRepository
before deploying to the real device, don't forget to sign your code (may take up to 2 weeks)
Now, implementing custom menu item:
class AdressBookMenuItem extends ApplicationMenuItem {
Contact mContact;
public AdressBookMenuItem(int order) {
super(order);
}
public Object run(Object context) {
if (context instanceof Contact) {
mContact = (Contact) context;
if (0 < mContact.countValues(Contact.TEL)) {
String phone = mContact.getString(Contact.TEL, 0);
PhoneArguments args = new PhoneArguments(
PhoneArguments.ARG_CALL, phone);
Invoke.invokeApplication(Invoke.APP_TYPE_PHONE, args);
} else {
Dialog.alert("This contact has no phone number");
}
}
return null;
}
public String toString() {
return "Call via ABC";
}
}
Now add it to the address book:
AdressBookMenuItem menuItem = new AdressBookMenuItem(0);
ApplicationMenuItemRepository repository =
ApplicationMenuItemRepository.getInstance();
long id = ApplicationMenuItemRepository.MENUITEM_ADDRESSBOOK_LIST;
repository.addMenuItem(id, menuItem);
Putting All Together
Run application
Press Call button
Select contact
Open menu
You should see
address book menu http://img9.imageshack.us/img9/8175/callviaabc.png
Tested on Bold 9000 simulator
Full code:
import javax.microedition.pim.Contact;
import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.PhoneArguments;
import net.rim.blackberry.api.menuitem.ApplicationMenuItem;
import net.rim.blackberry.api.menuitem.ApplicationMenuItemRepository;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class CallIntegrate extends UiApplication {
public CallIntegrate() {
pushScreen(new Scr());
}
public static void main(String[] args) {
CallIntegrate app = new CallIntegrate();
app.enterEventDispatcher();
}
}
class AdressBookMenuItem extends ApplicationMenuItem {
Contact mContact;
public AdressBookMenuItem(int order) {
super(order);
}
public AdressBookMenuItem(Object context, int order) {
super(context, order);
}
public Object run(Object context) {
if (context instanceof Contact) {
mContact = (Contact) context;
if (0 < mContact.countValues(Contact.TEL)) {
String phone = mContact.getString(Contact.TEL, 0);
PhoneArguments args = new PhoneArguments(
PhoneArguments.ARG_CALL, phone);
Invoke.invokeApplication(Invoke.APP_TYPE_PHONE, args);
} else {
Dialog.alert("This contact has no phone number");
}
}
return null;
}
public Contact getContact() {
return mContact;
}
public String toString() {
return "Call via ABC";
}
}
class Scr extends MainScreen {
public Scr() {
super(DEFAULT_MENU|DEFAULT_CLOSE);
String label = "Now please go to blackberry adressbook, "
+ "select contact and open menu";
add(new LabelField(label));
AdressBookMenuItem menuItem = new AdressBookMenuItem(0);
ApplicationMenuItemRepository repository =
ApplicationMenuItemRepository.getInstance();
long id = ApplicationMenuItemRepository.MENUITEM_ADDRESSBOOK_LIST;
repository.addMenuItem(id, menuItem);
}
}