ObjectChoiceField in blackberry - 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);
}
}
}

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.

Get Button click in each row in Blackberry

I want to get the button click event in each row. How to get that ?. I tried this link and its working if there is only one button in each row. But in my case, there are more than one button in each row. 10,20 and 11,21 are my buttons.
in RowManager class from the above link, i added the following code -
button = new ButtonField("1" + index, ButtonField.CONSUME_CLICK);
button.setCookie(new Integer(index));
button.setFont(textFont);
add(button);
button1 = new ButtonField("2" + index, ButtonField.CONSUME_CLICK);
button1.setCookie(new Integer(index));
button1.setFont(textFont);
add(button1);
Now on StackScreen class, public void fieldChanged(Field field, int context), How i get the name of the clicked buttons ?
Solved By My self -
public static int v=0;
button = new ButtonField("1" + index, ButtonField.CONSUME_CLICK);
button.setCookie(new Integer(v+1)); //set cookie
button.setFont(textFont);
add(button);
v=v+1; //increment the value of v
button1 = new ButtonField("2" + index, ButtonField.CONSUME_CLICK);
button1.setCookie(new Integer(v+1));
button1.setFont(textFont);
add(button1);
v=v+1;
and -
public void setChangeListener(FieldChangeListener listener) {
// only the button field supports change listeners
button.setChangeListener(listener);
button1.setChangeListener(listener);
}
Then on StackScreen class -
public void fieldChanged(Field field, int context) {
Object f=field.getCookie();
Dialog.alert("Button " +f);
}

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");
}
});

set text(number) to Edit Field through its setchangelistener

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);
}
}
});

Resources