listeners for ObjectChoice field - blackberry

I am creating a BlackBerry application which contains two ObjectChoiceFields. I want to add listeners for each of them. I searched for that but did not find any of useful code.
It is like a country-state selector on websites. I am looking for simple logic, and I have no need of any database oriented solution. Can anyone describe how to add listeners for those two ObjectChoiceField?
I have have attached my code below, but it is only working for country choice field. Nothing happens when I change state choice.
public class MyApplication extends MainScreen implements FieldChangeListener
{
ObjectChouceField choice_c,choice_s;
public MyApplication ()
{
this.setTitle("hai");
choice_c = new MyChoiceField("Select a Country", countryArray);
choice_c.setChangeListener(this);
this.add(choice_c);
choice_s = new MyChoiceField("Select a State", stateArray);
choice_s.setChangeListener(this);
this.add(choice_s);
..................
..................
}
public void fieldChanged(Field field, int context)
{
if(field == choice_category)
{
Dialog.alert("choice country has been pressed");
}
else if(field == choice_round)
{
Dialog.alert("choice state has been pressed");
}
}
}

Have you tried overriding the navigationClick method of the ObjectChoiceField?
Here is an example:
ObjectChoiceField selectionField = new ObjectChoiceField("Select the Country",countryArray)
{
protected boolean navigationClick(int arg0, int arg1)
{
return super.navigationClick(arg0, arg1);
}
};

cf is the ChoiceField. Use the getSelectedIndex to and trigger events accordingly.
cf.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
switch (cf.getSelectedIndex()) {
case 0:
//code here //
break;
default:
break;
}
}
});

Related

setChangeListener method is not invoked

i am new in blackberry developer. i am use a pillsetbutton and pillfieldbutton
but when i am click pillfieldbutton no any action is performed.I am using setchangeListener() method.but no any Action is performed.i am going Through this process.
public DemoPill() {
PillButtonSet objButtonSet=new PillButtonSet();
final PillButtonField objButtonField1=new PillButtonField("NSE");
final PillButtonField objButtonField2=new PillButtonField("BSE");
objButtonSet.add(objButtonField1);
objButtonSet.add(objButtonField2);
this.add(objButtonSet);
bjButtonSet.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("Hi ");
if(field==objButtonField1)
{
System.out.println("This Is NSE Button");
}
else if(field==objButtonField2)
{
System.out.println("This Is BSE Button");
}
}
});
}
}
You are printing it on console. So without debugging the code you will never know if your click is consumed. So just use an event thread to see the output on your screen. I have provided you the sample just check it. It will show the output on your screen. You can also use Dialog.inform(String message ) But its always good to do it on event thread.
public DemoPill() {
PillButtonSet objButtonSet=new PillButtonSet();
final PillButtonField objButtonField1=new PillButtonField("NSE");
final PillButtonField objButtonField2=new PillButtonField("BSE");
objButtonSet.add(objButtonField1);
objButtonSet.add(objButtonField2);
this.add(objButtonSet);
bjButtonSet.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// System.out.println("Hi ");
if(field==objButtonField1)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("objButtonField1 button clicked")
}
});
}
else if(field==objButtonField2)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("objButtonField2 button clicked")
}
});
}
}
});
}
}
May be this will help cheers. :)
You only can view the output of
System.out.println("ANYDATA");
in debug mode not in run.
Try to debug it not to run it.

Blackberry Bitmap fieldChanged throws IllegalStateException

I have a bitmap field in my blackberry5 app with fieldChanged listener attached to it which works absolutely fine
now my problem is that I also have an associated menu for the same purpose (I can not remove it's the requirement) and on click the menu I get a JVM 104 IllegalStateException
here is my menu class
public class TabMenu extends MenuItem{
MainScreen menuScreen;
Field button;
public TabMenu(String menuLabel,MainScreen menuScreen,Field button)
{
super(menuLabel, 1, 0);
this.menuScreen = menuScreen;
this.button = button;
}//end constructor
public void run()
{
FieldChangeListener listener = (FieldChangeListener)this.menuScreen;
listener.fieldChanged(this.button, this.button.getIndex());
this.button.setFocus();
}
}
and here is menu and fieldchnaged code
protected void makeMenu(Menu menu, int instance) {
menu.add(new RefreshMenu());
menu.addSeparator();
menu.add(new TabMenu("Go >", this, goTab));
menu.addSeparator();
}
public void fieldChanged(Field field, int context) {
if (field == goTab) {
Dialog.alert("goinf")
}
}
Try changing your TabMenu#run() method to the following:
public void run() {
this.button.fieldChangedNotify(this.button.getIndex());
this.button.setFocus();
}

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;

BlackBerry listfield development [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Can anybody suggest me how to create a clickable list field in blackberry such that on clicking one item a new screen must appear.
I've done a similar work with VerticalFieldManager and custom extended Field on JDE 5.0.
I assume that you have a list of objects that you wanted to display on main screen.
First; create a class which extends Field for your list's item, override its paint method, layout method and otuher events as your requirements.
Next, create a main screen that you wanted to show the list. Once you have populated your object list, in a loop, pass each object model to previously created field's constructor. Then add fieldChanged event to that field and add it to verticalFieldManager.
You need to override the events (like fieldChanged event) as you want to click on it and display its detail on another screen.
Finally, create a detail screen that takes required arguments to display your list item's object detail. On fieldChanged event of your main screen implementation, pass your object to detail screen and push the detail screen.
Also, this approach may be useful for you.
Example:
custom field:
public class CListItemField extends Field {
private CListItemModel model;
public CListItemField(CListItemModel _model, long style) {
super(style);
this.model = _model;
}
public CListItemModel getModel() {
return this.model;
}
// overrides
public int getPreferredHeight() {
//return custom height
}
public int getPreferredWidth() {
//return custom width
}
protected void layout(int width, int height) {
setExtent(Math.min(width, getPreferredWidth()), getPreferredHeight());
}
protected void paint(Graphics g) {
//custom paint stuff (borders, fontstyle, text position, icons etc.)
if (isFocus()) {
//focused item display settings
} else {
//item display settings
}
}
protected void drawFocus(Graphics graphics, boolean on) {
}
public boolean isFocusable() {
return true;
}
protected void onFocus(int direction) {
super.onFocus(direction);
invalidate();
}
protected void onUnfocus() {
super.onUnfocus();
invalidate();
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time) {
//send key event to listener
if (character == Keypad.KEY_ENTER) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}
list screen:
public class ScreenListbox extends MainScreen implements FieldChangeListener, FocusChangeListener {
private VerticalFieldManager verticalField;
private Vector listItemVector;
public ScreenOttoInbox(String title) {
super(title, Manager.NO_VERTICAL_SCROLL);
setData();
setComponents();
}
private void setData() {
//populate listItemVector according to your business (ie. read json response then parse it and collect it to a vector)
}
public void setComponents() {
verticalField = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
setListContent(verticalField, listItemVector);
add(verticalField);
}
private void setListContent(VerticalFieldManager field, Vector vector) {
try {
int vlen = vector.size();
for (int i = 0; i < vlen; i++) {
CListItemModel model = (CListItemModel) vector.elementAt(i);
CListItemField itemField = new CListItemField(model, Field.FOCUSABLE | Field.ACTION_INVOKE);
itemField.setChangeListener(this);
itemField.setFocusListener(this);
field.add(itemField);
}
} catch (Exception ex) { }
}
protected boolean onSavePrompt() {
return true;
}
public void fieldChanged(Field field, int context) {
//custom field's click/touch event handler
CListItemField itemField = (CListItemField) field;
ScreenItemDetail scrDetail = new ScreenItemDetail(itemField.getModel());
ScreenUtil.pushScreenWithLoader(scrDetail,true);
}
protected void onDisplay() {
super.onDisplay();
}
}
Create a class like below.
import java.util.Vector;
import net.rim.device.api.collection.util.SparseList;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
public class CListCallback implements ListFieldCallback {
private String[] resource;
private int rgb=Color.BLACK;
Vector elements;
Bitmap arraow;
public CListCallback(String[] resources){
this.resource=resources;
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
String text=(String) get(listField, index);
graphics.setColor(rgb);
graphics.drawText(text,60,y+25);
graphics.drawLine(0, y+59, DConfig.disWidth, y+59);
}
public Object get(ListField listField, int index) {
return resource[index];
}
public int getPreferredWidth(ListField listField) {
return DConfig.disWidth+10;
}
public int indexOfList(ListField listField, String prefix, int start) {
return -1;
}
}
And use above class in MainScreen class.
CListCallback clmenu=new CListCallback(arrayitems);
final ListField lf = new ListField(arraymenu.length) {
protected boolean keyChar(char character, int status, int time) {
if (character == Keypad.KEY_ENTER) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
protected boolean navigationUnclick(int status, int time) {
fieldChangeNotify(0);
return true;
}
};
lf.setCallback(clmenu);
lf.setRowHeight(60);
lf.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context)
{
int index=lf.getSelectedIndex();
UiApplication.getUiApplication.pushScreen(newNewScreen(imgarray[index]));
}
});
add(lf);
Thats it.it will work

how to add our menu item in messenger of blackberry

I want to add mymenuitem on smssending menulist,
How could I do that?
You just override the makeMenu method and add whatever you want.
protected void makeMenu(Menu menu, int instance) {
menu.add(getExitItem(0,0));
}
public MenuItem getExitItem(int ordinal, int priority) {
return new MenuItem("Close", ordinal, priority) {
public void run() {
System.exit(0);
}
};
}

Resources