Update text on a buttonfield when it is clicked - blackberry

I'm trying to set the text on a button when it is clicked. I'm initialising a BigVector which will update the text of the button with its value. I'm using a counter value to determine wihcih BigVector value should be selected. The problem is, the below code is expecting the counter value to be final.
A better methodology for updating the text on a Field when it is clicked is most welcome.
Here is my code -
final BigVector bigStringVectorA = new BigVector();
bigStringVectorA.addElement("A Test answer 1");
bigStringVectorA.addElement("A Test answer 2");
bigStringVectorA.addElement("A Test answer 3");
aAnswerOptionButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
}
});
Thanks

You can make the counter an instance variable, either in the outer class or in the anonymous FieldChangeListener:
aAnswerOptionButton.setChangeListener(new FieldChangeListener() {
private int counter = 0;
public void fieldChanged(Field field, int context) {
counter++;
if (counter > bigStringVectorA.size()) {
counter = 0;
}
aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
}
});

You can try to call the invalidate() method of that field and that should force the redraw of that button.
aAnswerOptionButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
aAnswerOptionButton.invalidate();
}
});

Related

Issue while using moving effect on slider

Here i use a value changing effect on a slider; if i move the slider, it will scroll and display updated values in an edit field, as per the moving of the slider bar. Also, there is an edit field effect where the slider should be moved as per the values entered into the edit field; but it is not working.
When i comment out part of the edit field effect, it is working properly but when i apply that edit field effect again, then it is not working ...
// for slider move
FieldChangeListener listenerslider1 = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
try {
if (field == serumosmslider) {
int serumosmslidervalue = serumosmslider.getValue();
String strplasmaslidervalue = Integer
.toString(serumosmslidervalue);
edtserumosm.setText(strplasmaslidervalue);
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
};
serumosmslider.setChangeListener(listenerslider1);
// for editfield
FieldChangeListener listenereditslider1 = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
hfmslider1.deleteAll();
String stredtweight = edtserumosm.getText().toString();
int editweight = Integer.parseInt(stredtweight);
SliderField theSlider = new SliderField(slider2thumb,
slider2progress, slider2base, slider2thumbfoc,
slider2progressfoc, slider2basefoc, 201,
editweight, 10, 10);
hfmslider1.add(theSlider);
hfmslider1.invalidate();
}
};
edtserumosm.setChangeListener(listenereditslider1);
You appear to be recreating your SliderField every time the EditField's value changes.
SliderField theSlider = new SliderField(slider2thumb,
slider2progress, slider2base, slider2thumbfoc,
slider2progressfoc, slider2basefoc, 201,
editweight, 10, 10);
hfmslider1.add(theSlider);
I don't think you want to do that. Just like you update the EditField text when the slider field changes, I think you should update the SliderField value when the EditField text is changed. So, something like this:
FieldChangeListener listenereditslider1 = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if (field == edtserumosm && context != FieldChangeListener.PROGRAMMATIC) {
String stredtweight = edtserumosm.getText().toString();
try {
int editweight = Integer.parseInt(stredtweight);
serumosmslider.setValue(editweight);
} catch (NumberFormatException nfe) {
// TODO: anything?
}
}
}
};
edtserumosm.setChangeListener(listenereditslider1);

Blackberry Java- Labelfield Listener. I want if anyone click a label then it should trigger a function/

I want if anyone click a label then it should trigger a function. Like I want if a user clicks a label then it should go to another page. Following is the code I tried.
Thanks in Advance!!
LabelField joinGroups = new LabelField("Join Groups",LabelField.FOCUSABLE ){
protected void layout(int width, int height) {
super.layout(width, height);
this.setExtent(1000, 44);
}
};
FChangeListener customListenerSurveys = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.alert("Surveys Clicked!");
}
};
joinGroups.setFocusListener(customListenerSurveys);
Try Navigation Clack.
LabelField joinGroups = new LabelField("Join Groups",LabelField.FOCUSABLE ){
protected boolean navigationClick(int status, int time){
Dialog.alert("Surveys Clicked!");
return true;
}
};

how to handle button and drop down field using fieldchangelistener

I want to display button and drop down on a same screen and want to capture it using fieldChnageListener ,i am not able to get how to add these both on the same screen.I have the following code :
ButtonField btnSubmit = new ButtonField("Submit!",
ButtonField.CONSUME_CLICK);
FieldListener listener = new FieldListener();
//assign that listener to the button
btnSubmit.setChangeListener(listener);
add(btnSubmit);
class FieldListener implements FieldChangeListener {
public void fieldChanged(Field f, int context){
//if the submit button is clicked
if (f == btnSubmit){
getCalender();
//if the EditField is empty
if(editField.getText().equals("")){
Dialog.alert("Please enter timezone in the field.");
}else{ // if it is not empty, display the message
Dialog.alert("TimeZone is"+editField.getText()+"!");
timeZone = editField.getText();
}
}
if(f == editField) {
//text changed in the ef-Field (EditField)
}
}
}
how to add a drop down here?
here how can you do this ..
If your class is implementing FieldChangeListner then in its fieldChanged method do like this
public class Demo extends mainScreen implements FieldChangeListener {
public Demo () {
ButtonField btnSubmit = new ButtonField("Submit!",
ButtonField.CONSUME_CLICK);
//assign that listener to the button
btnSubmit.setChangeListener(this);
add(btnSubmit);
}
public void fieldChanged(Field f, int context) {
//if the submit button is clicked
if (f == btnSubmit){
getCalender();
//if the EditField is empty
if(editField.getText().equals("")){
Dialog.alert("Please enter timezone in the field.");
}else{ // if it is not empty, display the message
Dialog.alert("TimeZone is"+editField.getText()+"!");
timeZone = editField.getText();
}
}
if(f == editField) {
//text changed in the ef-Field (EditField)
}
}
}
this should work.

listeners for ObjectChoice field

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

Click button on blackberry

How can I click a button field using the blackberry api? I'd like to mimic pressing a button as if the user pressed it.
Suppose you have this code (taken from the BB API doc):
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ButtonField buttonField = (ButtonField) field;
System.out.println("Button pressed: " + buttonField.getLabel());
}
};
ButtonField buttonField = new ButtonField("Test Button");
buttonField.setChangeListener(listener);
Then you can programmatically simulate a click by calling the fieldChangeNotify(int context) method of the buttonField. Note that you can distinguish normal/real click from a programmatic one by checking the context in the fieldChanged(Field field, int context). It is the same context you pass in fieldChangeNotify(int context).
Use EventInjector.NavigationEvent like this:
EventInjector.invokeEvent(new EventInjector.NavigationEvent(EventInjector.Navig ationEvent.NAVIGATION_CLICK, 0, 0, 0));
ButtonField buttonField = new ButtonField("Test Button" ,ButtonField.CONSUME_CLICK);
buttonField.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
Dialog.alert("Test Button Clicked");
}
});

Resources