1

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");
        }
    }
}
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Dil Se...
  • 877
  • 4
  • 16
  • 43
  • 1
    Your code looks Ok, unless there are some other details you have not posted. – Vit Khudenko Nov 30 '11 at 16:10
  • @arhimed: actually this was the code. after adding those choideFields, i had added one editfield and button. k i will check it once more and make u informed about that error clearly dear. Anyway thanks for ur comment. – Dil Se... Nov 30 '11 at 16:13
  • @BB Expert. It was actually the extented choicefield. – Dil Se... Nov 30 '11 at 16:14
  • @arhimed: you are correct dear. There were some problems with my logic. thank u dear. now it works alright. – Dil Se... Dec 01 '11 at 05:10

2 Answers2

3

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);
    }
};
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
Shadowtech
  • 376
  • 1
  • 4
  • 13
-2

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;
                }
            }
        });
baek
  • 425
  • 3
  • 7
  • So do i need to add the fieldChanged for every ChoiceField i add? is there any possibility for grouping them to a single function? like we do in java. if(e.getSource==button1){..} else if(e.getSource==button2){...} – Dil Se... Nov 30 '11 at 16:09
  • you can just create a common function like FieldChangeListener fc = new FieldChangeListner () and then assign this listener to all the choicefields. – baek Nov 30 '11 at 19:37
  • Sorry bhavik. I had tried as u answered. But while running it shown me an error, "The type MyApplication must implement the inherited abstract method FieldChangeListener.fieldChanged(Field, int)". And when i tried quick fix suggested by eclipse, another public void fieldChanged(Field field, int context) is adding at the end. – Dil Se... Dec 01 '11 at 04:59
  • You would be executing your code every time a user navigates through the choices – danielrvt-sgb Mar 01 '13 at 14:37