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
New Contributor
pravi4444
Posts: 2
Registered: ‎03-10-2013
My Device: blackberry 9930
Accepted Solution

ChoiceFiled field change listener Event getting called

Hello,

 

I have created one choicefield. and on field chagne listener i am showing dialog depending on condtion that balance is zero.

now my  problem is once dialog is shown then after that its showing dialog for every choice while i try to chagne the choice.

lets say on 3rd choice balance is zero then dialog is shown, on ok click of dialog 3rd selection is shwon in choice field.

Now when chagne the choice so i select the choice filed , and navigate throgh choice , then for every choice even if the balane is is greater than 0 for that choie still its showing dialog for zero balance.

please can you tell me the solution to check that old selected index 

How to check that choice selection is completed  Or this filed is under selection mode.

 

 

 

below is my code.

 

fDestination = new ObjectChoiceField("", spinnerArray, 0,Field.FIELD_LEFT) {
       protected void layout(int width, int height) {
      super.layout(fContext.getScreenWidth() / 3, 45);       setExtent(fContext.getScreenWidth() / 3, 45);

        }

};

 

fDestination.setChangeListener(new FieldChangeListener() {

      public void fieldChanged(Field field, int context) {
          try {

                  int dest = fDestination.getSelectedIndex() - 1;
                   if (dest != -1 ) {

                         if(balanceArray[dest].equalsIgnoreCase("0")) {
                                  Dialog.inform(fContext.getLabel("Balance is 0. select another destination"));
                     } 
             }

          } catch (ArrayIndexOutOfBoundsException ex) {

     } catch (NullPointerException ex) {

}


});

 

Below are my screen shots.

when i select any option where balance is not zero there its working fine but 

once choie selected for which  balance 0 dialog is shown.Then for any choice its shwing same popup message ,

 

 

Zero Balance Dialog         choice Filed

 

 

 

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: ChoiceField field change listener Event getting called

This question has been asked before and I have never had a very good answer.  In the past I used focuslisteners, and when the ChoiceField looses focus, then you check the value.  That works OK. 

 

But I had an idea when I reviewed your question.  The popup list you see is actually a popup screen.  Because it is displaying in front of your Field, it actually drives the onObscured() method and then drives the onExposed() method when the popup menu is removed.  So you can ignore changes while this is happening.  Then you only get the last change!  And it seems to work.  Here is some test code - it assumes you have the boolean variable

displayingChoices

as a global variable to this code:

 

Now in fact, onExposed and onObscured will get called in other circumstances, so to make this code bullet proof, you should probably check that the Field is in focus.  But I think it will work as is too.

 

Hope it works for you.

 

	    String [] options = new String [] { "1", "2", "3", "4" };
	    final ObjectChoiceField test = new ObjectChoiceField("Test: ", options) {
	    	public void onExposed() {
	    		displayingChoices = false;
	    	}
	    	public void onObscured() {
	    		displayingChoices = true;
	    	}
	    };
	    test.setChangeListener(new FieldChangeListener() {
	        public void fieldChanged(Field field, int context) {
	        	if ( displayingChoices ) {
	        		return;
	        	}
	        	Dialog.inform("Selected: " +test.getChoice(test.getSelectedIndex()));
	        }
	    });

 test is declared final just because it was easier. So this is not production code, it is just to demonstrate the idea.  Impement this in your own way. 

New Contributor
pravi4444
Posts: 2
Registered: ‎03-10-2013
My Device: blackberry 9930

Re: ChoiceField field change listener Event getting called

Hi Peter,  

Thanks a  ton .

This solutions worked awesome.

 

:Clap: , Thanks again.