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
Regular Contributor
Posts: 61
Registered: ‎11-08-2010
My Device: Not Specified

How to add dynamic ObjectChoiceField to a class

[ Edited ]

Hi ,

      Can anyone please say me how to add dynamic ObjectChoiceField to a class when a button is being clicked.

In my below code

Step 1 :  Have a menu item called "Add" .

Step 2 :  Have used 2 ObjectChoiceField.  

Step 3 :  Created a Vector and have added the 2 text to that vector . Have then passed that 2 text values from the vector to that ObjectChoiceField.

Step 4 : when the add MenuButton is being clicked , dynamically one ObjectChoiceField has to be added .

 

[CODE]

 

import

java.util.Vector;

 

import

net.rim.device.api.ui.Field;

import

net.rim.device.api.ui.FieldChangeListener;

import

net.rim.device.api.ui.MenuItem;

import

net.rim.device.api.ui.Screen;

import

net.rim.device.api.ui.UiApplication;

import

net.rim.device.api.ui.component.Menu;

import

net.rim.device.api.ui.component.ObjectChoiceField;

import

net.rim.device.api.ui.container.MainScreen;

 

 

public  class CarSelected extends MainScreen implementsFieldChangeListener {

 

           private ObjectChoiceField chgSelectFirstCar;

 

           private ObjectChoiceField chgSelectSecondCar;

 

           private ObjectChoiceField chgSelectThirdCar;

 

           private ObjectChoiceField chgSelectFourthCar;

 

           private ObjectChoiceField chgSelectFifthCar;

 

           privateintchgBusCount= 2;

        

              private ButtonField    bfAdd;

              private ButtonField bfCompare;

              private ButtonField bfBack ;

 

           private Vector vectorChgBuscontainer;

 

           private Vector vectorBusItem;

 

public  CarSelected() {

 

          vectorBusItem               =  newVector();

              vectorChgBuscontainer  =  newVector();

 

              chgSelectFirstCar         =  new ObjectChoiceField("Select 1st Car :", null);

              chgSelectSecondCar    =   new ObjectChoiceField("Select 2nd Car :", null);

              chgSelectThirdCar        =   new ObjectChoiceField("Select 3rd Car :", null);

              chgSelectFourthCar      =   new ObjectChoiceField("Select 4th Car :", null);

              chgSelectFifthCar         =   new ObjectChoiceField("Select 5th Car :", null);

   

           bfAdd  = new ButtonField("Add",ButtonField.CONSUME_CLICK);

              bfAdd.setChangeListener(this);

              bfCompare = new ButtonField("Compare",ButtonField.CONSUME_CLICK);

              bfCompare.setChangeListener(this);

              bfBack = new ButtonField("Back",ButtonField.CONSUME_CLICK);

              bfBack.setChangeListener(this);

 

              add(bfAdd);

              add(bfCompare);

              add(bfBack);

 

           for(inti = 0;i<5; i++) {

                          vectorChgBuscontainer.addElement(chgSelectFirstCar) ;

                          vectorChgBuscontainer.addElement(chgSelectSecondCar) ;

                          vectorChgBuscontainer.addElement(chgSelectThirdCar) ;

                          vectorChgBuscontainer.addElement(chgSelectFourthCar) ;

                          vectorChgBuscontainer.addElement(chgSelectFifthCar) ;

              }

 

          vectorBusItem.addElement("Sunil");

             vectorBusItem.addElement("Kumar");

 

          

         for(int i=0;i<vectorBusItem.size();i++){

                   for(int j=0;j<vectorChgBuscontainer.size();j++){

 

                            String []dataTwo =   new String[vectorBusItem.size()];

 

                            vectorBusItem.copyInto(dataTwo);

                            ((ObjectChoiceField)vectorChgBuscontainer.elementAt(j)).setChoices(dataTwo);

 

                   }

 

         }

 

       for(int i=0;i<chgBusCount;i++) {

                   dataThree = new String[vectorChgBuscontainer.size()];

                      vectorChgBuscontainer.copyInto(dataThree);

                      UiApplication.getUiApplication().pushScreen(new CarSelectedTwo(dataThree));  

         }

                  

}

 

public void fieldChanged(Field field, intcontext) {

                         

          if (field == bfAdd){

                      chgBusCount = chgBusCount+ 1;

                      UiApplication.getUiApplication().pushScreen(new CarSelected());

           }

       else if(field == bfCompare) {

 

         }

       elseif(field == bfBack) {

 

        }

                   

}

 

}

 

[/CODE]

 

Can anyone please help me.

 

With regards,

Sunil

 

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

Re: How to add dynamic ObjectChoiceField to a class

Normally when a button is clicked, it is picked up by a FieldChangeListener, in the fieldChanged(..) method.

 

This method is run with the Event Thread, so it possible for you, in that method, to just 'add' a Field to the screen, or in fact delete a Field from the screen.

 

Does this answer the question?

Highlighted
Regular Contributor
Posts: 61
Registered: ‎11-08-2010
My Device: Not Specified

Re: How to add dynamic ObjectChoiceField to a class

[ Edited ]

Hi Peter ,

      Thank you for  your reply .  Have done some modifications to the above code. Please check it . Say have removed the menu item and have given it in FieldCahnged as u said. But still am not getting it .

Can you please say me where i went wrong .

 

With Regards ,

Sunil

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

Re: How to add dynamic ObjectChoiceField to a class

"Have done some modifications to the above code. Please check it "

 

Where are the modifications?

 

I would have expected something in here:

 

if (field == bfAdd){
chgBusCount = chgBusCount+ 1;
// ????????
UiApplication.getUiApplication().pushScreen(new CarSelected());
}

 

I'm not sure if you are adding a new element to the Choices or adding a new ChoiceField either.  But you don't do either in the fieldChanged method that I can see. 

Regular Contributor
Posts: 61
Registered: ‎11-08-2010
My Device: Not Specified

Re: How to add dynamic ObjectChoiceField to a class

Hi ,

    The modifications which i have done is in the Constructor part . In that for loop  .

 

With regards,

Sunil

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

Re: How to add dynamic ObjectChoiceField to a class

Sorry I have no idea what was there before and what you have changed.  I'm still not clear what your problem is - are you adding a choice or a choice field. Regardless, the position I suggested is where you should put this addition and I don't see any code there.  So look at that place and figure out if you want to add a Field to the screen or change the choices in an existing Field.  I think the former, in which case it is simply a matter of adding the next Field.  Try it and see. 

Developer
Posts: 246
Registered: ‎12-13-2010
My Device: Not Specified

Re: How to add dynamic ObjectChoiceField to a class

You need to "add" the component when the button is clicked.

Ex:

add(chgSelectFirstCar);
Regular Contributor
Posts: 61
Registered: ‎11-08-2010
My Device: Not Specified

Re: How to add dynamic ObjectChoiceField to a class

[ Edited ]

Hi ,

     Let me explain it once again. In my above application have initialised 5 ObjectChoiceField  and have added it to the vector say as given below .

 

for(inti = 0;i<5; i++) {

                   vectorChgBuscontainer.addElement(chgSelectFirstCar) ;

                   vectorChgBuscontainer.addElement(chgSelectSecondCar) ;

                   vectorChgBuscontainer.addElement(chgSelectThirdCar) ;

                   vectorChgBuscontainer.addElement(chgSelectFourthCar) ;

                   vectorChgBuscontainer.addElement(chgSelectFifthCar) ;

}

 

          ->  But in my View page i have added only the 1st and the 2nd ObjectChoiceField .Where the rest of the 3 ObjectChoiceField  has to be added dynamically when the Add Button is being clicked.

         ->   2 text values is being added to a vector say vectorBusItem . And it is these 2 text values that comes under a  ObjectChoiceField  .

         ->  Where this works fine as i am getting those 2 text values under the ObjectChoiceField  .

 

vectorBusItem.addElement("Sunil");

vectorBusItem.addElement("Kumar");

 

for(int i=0;i<vectorBusItem.size();i++){

      for(int j=0;j<vectorChgBuscontainer.size();j++){

                      dataTwo  =  new String[vectorBusItem.size()];

                      vectorBusItem.copyInto(dataTwo);

                     ((ObjectChoiceField)vectorChgBuscontainer.elementAt(j)).setChoices(dataTwo);

         }

}

 

add(chgSelectFirstCar);

add(chgSelectSecondCar);

 

the problem is when i click the add button an additional ObjectChoiceField  is not being added and i get an error.

Have given it in the below way:

 

private intchgBusCount = 2;

//have given the chgBusCount as 2 initially as first i show nly the first 2 ObjectChoiceField .

 

public void fieldChanged(Field field, int context) { 

 

        if(field == bfAdd) {

                chgBusCount = chgBusCount+ 1;

                      //UiApplication.getUiApplication().pushScreen(new CarSelected().deleteAll());

                for(int i=0;i<chgBusCount;i++) {

                           dataTwo = new String[vectorChgBuscontainer.size()];

                                    vectorChgBuscontainer.copyInto(dataTwo);

                                    ((ObjectChoiceField)vectorChgBuscontainer.elementAt(i)).setChoices(dataTwo);

                                    UiApplication.getUiApplication().pushScreen(new CarSelected(dataTwo));

                }

                  //UiApplication.getUiApplication().pushScreen(new CarSelectedTwo(dataThree));

 }

}

 

With regards,

Sunil

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

Re: How to add dynamic ObjectChoiceField to a class

Let me explain once again what was said previously. 

 

In the fieldChange method, when you want to add a Field to the screen, you can just add it.  So if you wish to add the 3rd one, you can just code

add(chqSelectThirdCar);

This will be added on the end of the current screen. 

 

If you wish to add it in a specific place, then you can use insert, or get clever with Managers.  If you want to delete, you can just delete the Field from its Manager.  If you want to find out if a Field has been added or not, you can check to see if is has a Manager. 

 

I suspect this is enough information for you to achieve what you are trying to achieve. 

 

If it is not, rather than pasting your full code, can you create a simpler example and perhaps just ask one question at a time - I'm still not clear on what the Vector busItems has to do with anything. 

 

I guess I could become clearer if I had time to closely review your code, but, like a lot of other people who help on this forum, I do this in my own time, so the easier you make it for us, the more chance you have of getting a good and quick answer.