Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 237
Registered: ‎07-20-2010
My Device: Not Specified
Accepted Solution

ObjectChoiceField and ChoiceField$ChangeOptionChoiceField classcastexception

[ Edited ]

one the line in my ObjectChoiceField's fieldchangelistener I have this line:

 final int choice = ((ObjectChoiceField) field).getSelectedIndex();

 but it gives this exception:

ClassCastException: Cannot cast net.rim.device.api.ui.component.ChoiceField$ChangeOptionChoiceField (id=91958272) to net.rim.device.api.ui.component.ObjectChoiceField

 

How can I get the ObjectChoiceField's newly selected index from inside the listener?

I have tried using a final ObjectChoiceField so I could just access it inside the listener but that way I still get the previous value for the selected index.

 

my code:

                    final ObjectChoiceField distance = new ObjectChoiceField("Choose Menu:",menuList);
               

                    if (menuList.length > 1) {
                    //    Dialog.alert("length="+menuList.length);
                    //    menuid=Integer.toString(menuList[Dialog.ask("Which menu?",menuList,0)].getValue());
                    //    Dialog.alert("menuid="+menuid);
                        distance.setChangeListener(new    FieldChangeListener( ){
                            public void fieldChanged(Field field, int context) {
                                //final int choice = ((ObjectChoiceField) field).getSelectedIndex();
                                Dialog.alert("get choice next");
                                final int choice =  distance.getSelectedIndex();
                                Dialog.alert("get choice="+choice);
                                //field.get
                                final int ctx = context;
                                new Thread() {
                                    public void run() {
                                        loadMenus(ctx, choice);
                                    }
                                }.start();
                            }
                        });

Developer
Posts: 203
Registered: ‎07-30-2009
My Device: Bold(9780),Curve(8520)
My Carrier: Vodafone

Re: ObjectChoiceField and ChoiceField$ChangeOptionChoiceField classcastexception

HI,

 

please try below code for that

 

ObjectChoiceField distance = new ObjectChoiceField("Choose Menu:",null);
        distance.setChangeListener(new FieldChangeListener(){
            public void fieldChanged(Field field, int context) {
                if(field instanceof ObjectChoiceField){
                    ObjectChoiceField distance = (ObjectChoiceField)field;
                    int choice = distance.getSelectedIndex();
                }
            }

        });

 

Regards,

Tita

Developer
Posts: 468
Registered: ‎08-07-2009
My Device: Curve, Bold, Storm
My Carrier: Blackberry Developer

Re: ObjectChoiceField and ChoiceField$ChangeOptionChoiceField classcastexception

            objectSymbol = new ObjectChoiceField("", ARRAY, 0,Field.FIELD_LEFT)
            {
                protected void fieldChangeNotify(int context)
                {
                    indexCurrency = getSelectedIndex();
                }
            };

 

 

----------------------------------------------------------
feel free to press the kudos button on the left side to thank the user that helped you.
please mark posts as solved if you found a solution.
Highlighted
Developer
Posts: 237
Registered: ‎07-20-2010
My Device: Not Specified

Re: ObjectChoiceField and ChoiceField$ChangeOptionChoiceField classcastexception

Thank you both for those suggestions, while I am not sure why such measures should be necessary I am glad I finally  got past this.  My final code is:

final ObjectChoiceField menus = new ObjectChoiceField("Choose Menu:",menuList,0, ObjectChoiceField.FIELD_LEFT){
     protected void fieldChangeNotify(int context){ 
	 final int choice = getSelectedIndex();
          final int ctx = context;
	 new Thread() {
	        	 public void run() {
	                loadMenus(ctx, choice);
	          }
	 }.start();
     }
};

 Thank you again!!