0

BasicEditField从. _ _ObjectChoiceField

问题:BasicEditField我添加到屏幕的字段不会刷新,除非我在我的ObjectChoiceField.

我想做的事 :

  1. 选择BasicEditFields我想要的号码。
  2. 刷新屏幕以显示添加的字段。

PD:如果您需要更多信息,请告诉我,对不起我的英语。我是为 BlackBerry 平台开发的新手

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]);
            }

     }

}
4

1 回答 1

2

您确实应该ObjectChoiceField在侦听器中添加或删除字段。那时您就知道正确的字段数是多少。(当然,如果您只是想保持代码整洁,您可以定义一个单独的方法,从选择字段侦听器中调用......这没什么不同)。

尝试这样的事情:

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);
               }                                         
            }
         }
      });
   }

我定义了一个名为 的新成员fields,它使跟踪基本编辑字段变得更加容易(以防这个屏幕也有许多其他字段)。

当调用选择字段监听器时,我确定用户想要多少个字段;如果他们需要更多,我将它们添加到屏幕和fields向量中。如果他们想要更少,我会从 Vector 的末尾删除一些字段,并将它们从屏幕中删除。

注意:这里应该不需要调用invalidate()。调用Screen#add()Screen#delete()应该添加/删除字段并导致重新绘制。

于 2013-11-12T07:36:40.180 回答