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: 39
Registered: ‎04-09-2009
My Device: Not Specified

ObjectChoiceField - detecting change of selection

[ Edited ]

 

Hi guys.  I know this has been covered before but I have read many threads and never saw a definitive answer.   I have an ObjectChoiceField with four items.  When the user changes the selection, I need to take action and show some new stuff on the screen.

 

My problem is the same as in:

 

http://supportforums.blackberry.com/t5/Java-Development/ObjectChoiceField-and-FieldChangeListener/m-...

 

In that thread the suggestion is to use onUnfocus().  I tried it.  The trouble is, the user expects the change to occur right after the selection click, not later after the focus is moved off the choice field.

 

If you override fieldChanged() on a listener, or fieldChangedNotify() on the ObjectChoiceField, you have to distinguish between different calls.

 

My best success was with fieldChangedNotify().  It gets called during initialization, triggered by setChoices() and setSelectedIndex(), with

 

context == FieldChangeListener.PROGRAMMATIC

 

Then, when I click on the currently selected string, to pop out the choice list, it does not get called -- good!

 

Finally, if I click on a new item to select it, fieldChangedNotify() is called with:

 

context == 2

 

Thus, it looks like I can base my action on

 

if (context != FieldChangeListener.PROGRAMMATIC) ...

 

 

Can you tell me if I can rely on this behavior (eg, across different models and OS versions)?  What exactly does "PROGRAMMATIC" mean?  Will other unexpected things with context != PROGRAMMATIC trigger my action?

 

Thanks --fd.

(ps, I ran the spell checker before posting and it suggested: onUnfocus ---> unfrock, and fieldChanged --> flagellation.  Hmmm.....)

 

Highlighted
New Developer
Posts: 14
Registered: ‎08-13-2008
My Device: Not Specified

Re: ObjectChoiceField - detecting change of selection

The other thing you can do is cache the current selected index of the ObjectChoice field. The selected index does not change until the user actually selects it. So you can tell when a choice has actually been made when the selected index differs than that of the cached selected index.

Contributor
Posts: 17
Registered: ‎02-25-2010
My Device: Bold 9700 OS 5.0.0.593
My Carrier: Vodafone Spain

Re: ObjectChoiceField - detecting change of selection

With this, only when the user change the option, the FieldChangeListener is called.

 

 _codigospostales=new MyObjectChoiceField("CP: ",ListaClientes.getCodigosPostales("Todas"),0,Field.FIELD_LEFT|Field.USE_ALL_WIDTH|DrawStyle.ELLIPSIS|Field.FIELD_TOP);

 

_codigospostales.setChangeListener(new CambioCP());

   

 

private class CambioCP implements FieldChangeListener{
        public CambioCP(){
        }
        public void fieldChanged(Field _field, int _context){
            //context=2 Cambio y aceptado
            if (_context==2){
                //Tengo que aplicar el filtro por poblaciones con el texto buscado
                _txtCodigo="";
                _listcallBack=new ListCallback(ListaClientes.getListaNombres(_txtBusqueda,(String)(_poblaciones.getChoice(_poblaciones.getSelectedIndex())),(String)(_codigospostales.getChoice(_codigospostales.getSelectedIndex()))));
                _lista.setSize(_listcallBack.size());
                _lista.setCallback(_listcallBack);
                _lista.invalidate();
                _status.setText(_resource.getString(IdiomasResource.Buscar)+": "+_txtBusqueda);
                setStatus(_status);           
            }       
        }
    }

----------------------------------------------
caelul // Spanish Developer
-------------------------------------------
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: ObjectChoiceField - detecting change of selection

I have code that relies on detecting changes using a FieldChangeListener, but ignoring programmatic changes using (context != FieldChangeListener.PROGRAMMATIC) and it works in all OS levels and on all devices I have tried it on. 

 

Other than setSelectedIndex and setChoices, I am not aware of anything else that will trigger a PROGRAMMATIC change. 

Contributor
Posts: 30
Registered: ‎12-02-2010
My Device: BlackBerry 8520
My Carrier: Mobilink

Re: ObjectChoiceField - detecting change of selection

Hi All..!

 

I m using two ObjectChoiceField naming first, second and sending an HTTP Request on first's fieldChangeNotify() which will setChoices() of the second.

 

My problem is the short keys.

On the screen when focus is on first,  if the user presses any key the field value will be changed according to key.

As the change occurs, an HTTP request is sent.

 

Everything working fine, but if the user press the keys immediately for two or more times, number of requests are sent to the server.

 

How to manage the fieldChangeNotify and getting the desired item to be sent in http request for one at a time..

Adil Soomro