set text(number) to Edit Field through its setchangelistener - blackberry

Hi I want to accept number in edit field up to two decimal.So I am setting listener to it Then I
am checking whether number is two decimal or more and if it is more than two decimal then i am
truncating the number and again trying to set the truncated number.But it showing 104 error at this place interestRate.setText(text).My code is
interestRate=new EditField();
interestRate.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
String text=interestRate.getText().toString();
code here--
interestRate.setText(text);
}
};
So my question is whether text can be set from its listener or not

Looks like you just need to use some condition check in order not to get in an infinite loop:
interestRate=new EditField();
interestRate.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
String text = interestRate.getText().toString();
// code here to create a truncated text
if (!truncated.equals(text)) {
// next time we will not get here
// because truncated will be equal to text
interestRate.setText(text);
}
}
});

Related

How to create a number of Fields dynamically in Blackberry Java SDK 5.0?

I'm trying to create a couple of BasicEditField objects after i get the number of fields that i want from an ObjectChoiceField.
Problem: the BasicEditField fields that i add to my screen don't refresh unless i do it in the listener from my ObjectChoiceField.
what i want to do :
select the number of BasicEditFields that i want.
refresh the screen so the fields added appear.
PD: if you need more info, just tell me, and sorry about my english. I'm new at developing for the BlackBerry plataform
public final class MyScreen extends MainScreen
{
private int fields_lenght;
public MyScreen()
{
// Set the displayed title of the screen
setTitle("Example");
fields_lenght =0;
final String shortcodes[] = {"1","2","3"};
final ObjectChoiceField dropdownlist=new ObjectChoiceField("Select a number of fields",shortcodes);
this.add(dropdownlist);
dropdownlist.setChangeListener( new FieldChangeListener() {
public void fieldChanged( Field arg0, int arg1 ) {
if(arg1 != PROGRAMMATIC){
fields_lenght= Integer.parseInt(shortcodes[dropdownlist.getSelectedIndex()]);
}
}
} );
// how to refresh the screen with the new fields ???
BasicEditField fields[]=new BasicEditField [fields_lenght] ;
for(int i = 0; i<fields.length;i++){
fields[i]=new BasicEditField("Campo "+i,"");
this.add(fields[i]);
}
}
}
You really should add or delete the fields from within your ObjectChoiceField listener. That's when you know what the proper number of fields is. (Certainly, if you just want to keep your code neat and clean, you could define a separate method, that is called from the choice field listener ... that's not much different).
Try something like this:
public final class MyScreen extends MainScreen {
/** A cached vector of the BasicEditFields, to make deleting easier */
private Vector fields;
public MyScreen() {
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
setTitle("Example");
final String shortcodes[] = {"1","2","3"};
final ObjectChoiceField dropdownlist = new ObjectChoiceField("Select a number of fields", shortcodes);
add(dropdownlist);
fields = new Vector();
final Screen screen = this;
dropdownlist.setChangeListener( new FieldChangeListener() {
public void fieldChanged( Field field, int context ) {
if (context != PROGRAMMATIC) {
// how many fields has the user chosen?
int fieldsLength = Integer.parseInt(shortcodes[dropdownlist.getSelectedIndex()]);
while (fieldsLength > fields.size()) {
// we need to ADD more fields
Field f = new BasicEditField("Campo " + fields.size(), "");
fields.addElement(f);
screen.add(f);
}
while (fieldsLength < fields.size()) {
// we need to DELETE some fields
Field f = (Field)fields.elementAt(fields.size() - 1);
fields.removeElement(f);
screen.delete(f);
}
}
}
});
}
I defined a new member named fields, which just makes it easier to keep track of the basic edit fields (in case this screen has many other fields, too).
When the choice field listener is called, I determine how many fields the user wants; if they need more, I add them to the screen, and to the fields Vector. If they want fewer, I delete some fields from the end of the Vector, and remove them from the Screen.
Note: there should be no need to call invalidate() here. Calling Screen#add() or Screen#delete() should add/delete the fields and cause repainting.

BB java objectchoicefiled should get display autmatically on selecting one value in other objectchoicefield i.e onchange functionality

Hi am trying to display objectchoicefield by set of data in array.by selecting that comparing with other array automatically another objectchoicefield should get display with corresponding matches.i.e, onchange function, the other objectchoicefield should get load with values.guide me.
Document document=generalXmlAccess.access(generalXmlAccess.getArea());
NodeList list2=document.getElementsByTagName("tuple");
final String[] area = new String [list2.getLength()];
final String[] areaid = new String [list2.getLength()];
for(int i=0;i<list2.getLength();i++)
{
NodeList list=document.getElementsByTagName("NAME");
NodeList list3=document.getElementsByTagName("ROW_ID");
area[i]=list.item(i).getFirstChild().toString()+"-"+list3.item(i).getFirstChild().toString();
areaid[i]=list3.item(i).getFirstChild().toString();
}
final ObjectChoiceField choiceField=new ObjectChoiceField("Select Area",area);
choiceField.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if(field.equals(choiceField))
{
int index=choiceField.getSelectedIndex();
String values=areaid[index].toString();
Document document1=generalXmlAccess.access(generalXmlAccess.getSubArea());
NodeList list3=document1.getElementsByTagName("tuple");
for(int i=0;i<list3.getLength();i++)
{
subareaid=new String[list3.getLength()];
NodeList nodeList=document1.getElementsByTagName("PAR_ROW_ID");
if(nodeList.item(i).getFirstChild().toString().equals(values))
{
NodeList list=document1.getElementsByTagName("NAME");
subareaid[i]=list.item(i).getFirstChild().toString();
}
add(new ObjectChoiceField("Subarea", subareaid));
}
}
}
});
choiceField.setFont(font1);
I think, following steps might guide you.
Step 1: Create first objectchoicefield with its data.
Step 2: Implement its field change listener, in which you will use the selected value of first choicefield as parameter to get the array of corresponding values to be set to second objectchoicefield ..

Not able to set a common Listener in BlackBerry development

I am trying to set a common listener for an Customized button and Bitmap field.I am able to reach in listener but not able to differentiate between two fields.
private class MeaningsDetailsPageListner implements FieldChangeListener{
public void fieldChanged(Field field, int arg1) {
Dialog.alert("Hi");
if(field == bField){
Dialog.alert("Image Clicked");
}else if(field == wordBtn){
Dialog.alert("Button Clicked!!");
}
}
}
In following code wordBtn is my customised button and other is BitmapField.I am getting Hi alert but not able to differentiate further.
Any help would be appreciated.
Although I see what you're trying to do, you're better off adding a FieldChangeListener to each Field individually as an anonymous class. This way you don't have to worry about casting your Field to the correct type when testing for equality inside fieldChanged.
ButtonField b = new ButtonField("Hello!");
b.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.alert("Button clicked");
}
});

ObjectChoiceField in blackberry

In my application there is one ObjectChoiceField in MainScreen
i want to get change Listner of ObjectChoiceField after click on index 0.
i had already get click event of ObjectChoiceField after click on index grater than 1 and so on ..
so how can i get instance click event after click on ObjectChoiceField ?
I am not really sure what you are asking for.
In your FieldChangeListener.fieldChanged() method, calling ObjectChoiceField.getSelectedIndex() tells you which index is currently selected. You can look for index 0 from that.
If that is not what you need, then you need to clarify your question better.
ObjectChoiceField choiceField = new ObjectChoiceField();
public void fieldChanged(Field field, int context) {
if(field.equals(choiceField))
{
if(choiceField.getSelectedIndex==0)
{your Code}
}
If you want the value which the user have selected you can do by this way too
ObjectChoiceField choiceField = new ObjectChoiceField();
public void fieldChanged(Field field, int context) {
if(field.equals(choiceField))
{
if(choiceField.getSelectedIndex==0)
{
int index = choiceField.getSelectedIndex();
String s = (String) objectWeather.getChoice(index);
Dialog.inform("selected value is"+s);
}
}
}

Move Focus From PasswordEditField to Next PasswordEditField in blackberry

I am new to BlackBerry and Java. I am trying to figure out but not getting any correct way to implemnt my task. I want to enter 16 digit password. So for that, I have four passwordEditField in HorizontalFieldManager, each passwordEditField allows max 4 digits. When I enter 4 digits in first leftmost passwordEditField, I want to set focus automatically to the following next passwordEditField without any keypress. I used,
passwordEditField = new PasswordEditField("","",4,0){
protected void onUnfocus()
{
this.setFocus(passwordEditField.getIndex()+1,0,0);
}
};
I tried moveFocus(), setFocus(), setCursorPosition() but not getting the focus on to next element. Is there any way i can impelement this task in blackberry.
passwordEditField.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
String text = passwordEditField.getText().toString();
if (text.length() == 4) {
nextPasswordEditField.setFocus();
}
}
});

Resources