0

I'm trying to create a couple of BasicEditField objects after i get the number of fields that i want from an ObjectChoiceField.

Problem: the BasicEditField fields that i add to my screen don't refresh unless i do it in the listener from my ObjectChoiceField.

what i want to do :

  1. select the number of BasicEditFields that i want.
  2. refresh the screen so the fields added appear.

PD: if you need more info, just tell me, and sorry about my english. I'm new at developing for the BlackBerry plataform

public final class MyScreen extends MainScreen
{
    private int fields_lenght;

    public MyScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("Example");
        fields_lenght =0;

        final String shortcodes[] = {"1","2","3"};
        final ObjectChoiceField dropdownlist=new ObjectChoiceField("Select a number of fields",shortcodes);
        this.add(dropdownlist);

        dropdownlist.setChangeListener( new FieldChangeListener() {

            public void fieldChanged( Field arg0, int arg1 ) {
                    if(arg1 != PROGRAMMATIC){
                        fields_lenght= Integer.parseInt(shortcodes[dropdownlist.getSelectedIndex()]);
                    }
            }
        } );

        // how to refresh the screen with the new fields ???

            BasicEditField fields[]=new BasicEditField [fields_lenght] ;

            for(int i = 0; i<fields.length;i++){
                fields[i]=new BasicEditField("Campo "+i,"");
                this.add(fields[i]);
            }

     }

}
Nate
  • 31,017
  • 13
  • 83
  • 207
AmirG
  • 615
  • 8
  • 18
  • Can you show us the code where you are adding your new BasicEditFields? (just click the **edit** link below the question and add the code into your question) Also, is there a reason why you **don't** want to do it in the ObjectChoiceField listener? – Nate Nov 11 '13 at 23:31
  • @Nate Hello Nate well now you can see an example of what i want to do. – AmirG Nov 12 '13 at 03:04
  • @Nate I didnt want to use it in the ObjectChoiceField listener because it add more fields than i need since the listener take the trackwheel click and movement as the same action, i tryed to override trackwheel click and movement but it didnt work. Also i would like to delete the fields , for example if the choice change to 2 from 3 wich means i would have to delete one field. I hope i explained myself. i read about Invalidate() method from the screen but i dont really understand how to implement it. – AmirG Nov 12 '13 at 03:11

1 Answers1

2

You really should add or delete the fields from within your ObjectChoiceField listener. That's when you know what the proper number of fields is. (Certainly, if you just want to keep your code neat and clean, you could define a separate method, that is called from the choice field listener ... that's not much different).

Try something like this:

public final class MyScreen extends MainScreen {

   /** A cached vector of the BasicEditFields, to make deleting easier */
   private Vector fields;

   public MyScreen() {
      super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);

      setTitle("Example");

      final String shortcodes[] = {"1","2","3"};
      final ObjectChoiceField dropdownlist = new ObjectChoiceField("Select a number of fields", shortcodes);
      add(dropdownlist);

      fields = new Vector();
      final Screen screen = this;

      dropdownlist.setChangeListener( new FieldChangeListener() {

         public void fieldChanged( Field field, int context ) {
            if (context != PROGRAMMATIC) {
               // how many fields has the user chosen?
               int fieldsLength = Integer.parseInt(shortcodes[dropdownlist.getSelectedIndex()]);

               while (fieldsLength > fields.size()) {
                  // we need to ADD more fields
                  Field f = new BasicEditField("Campo " + fields.size(), "");
                  fields.addElement(f);
                  screen.add(f);
               }

               while (fieldsLength < fields.size()) {
                  // we need to DELETE some fields
                  Field f = (Field)fields.elementAt(fields.size() - 1);
                  fields.removeElement(f);
                  screen.delete(f);
               }                                         
            }
         }
      });
   }

I defined a new member named fields, which just makes it easier to keep track of the basic edit fields (in case this screen has many other fields, too).

When the choice field listener is called, I determine how many fields the user wants; if they need more, I add them to the screen, and to the fields Vector. If they want fewer, I delete some fields from the end of the Vector, and remove them from the Screen.

Note: there should be no need to call invalidate() here. Calling Screen#add() or Screen#delete() should add/delete the fields and cause repainting.

Nate
  • 31,017
  • 13
  • 83
  • 207
  • wow thanks Nate i don't know what i was doing that inside of listener he understand the trackwheel movement as a trackwheel click so even when he just navigated by the list it chose something. Anyways the code that you gave me worked fine , thanks bro have a nice day! – AmirG Nov 12 '13 at 12:03