【问题标题】:BlackBerry - Add items to a ListFieldBlackBerry - 将项目添加到 ListField
【发布时间】:2008-12-26 16:32:59
【问题描述】:

谁能给我一个简单的例子,说明如何将三行添加到 ListField 以便列表显示类似这样的内容?

项目 1

第 2 项

第 3 项

我只想显示一个列表,用户可以在其中选择一个项目,程序会根据所选项目做一些事情。

我在整个互联网上进行了搜索,但似乎不可能找到一个简单的例子来说明如何做到这一点(我发现的大多数例子都不完整)而且黑莓文档很糟糕。

谢谢!

【问题讨论】:

    标签: user-interface blackberry listfield


    【解决方案1】:

    您可能想看看如何使用 ObjectListField。处理选择操作是通过包含的 Screen 对象完成的,我在下面使用 MenuItem 完成了此操作,我不确定如何设置默认选择侦听器,您可能必须检测键和滚轮事件。

    为您提供一些示例代码:(未测试!)

    MainScreen screen = new MainScreen();
    screen.setTitle("my test");
    
    final ObjectListField list = new ObjectLIstField();
    String[] items = new String[] { "Item 1", "Item 2", "Item 3" };
    list.set(items);
    
    screen.addMenuItem(new MenuItem("Select", 100, 1) {
        public void run() {
            int selectedIndex = list.getSelectedIndex();
            String item = (String)list.get(selectedIndex);
            // Do someting with item
        });
    screen.add(list);
    

    【讨论】:

      【解决方案2】:

      您可以像这样覆盖 navigationClick 方法:

      ObjectListField list = new ObjectListField()
      {
          protected boolean navigationClick(int status, int time)
          {
              // Your implementation here.
          }
      };
      

      【讨论】:

        【解决方案3】:
        final class SimpleListScreen extends MainScreen
        {
            public SimpleListScreen()
            {
                super(Manager.NO_VERTICAL_SCROLL);
        
                setTitle("Simple List Demo");
        
                add(new LabelField("My list", LabelField.FIELD_HCENTER));
                add(new SeparatorField());
        
                Manager mainManager = getMainManager();
        
                SimpleList listField = new SimpleList(mainManager);
        
                listField.add("Item 1");
                listField.add("Item 2");
                listField.add("Item 3");
                }
            }
        
            //add listener so that when an item is chosen,it will do something
        

        【讨论】:

          【解决方案4】:
          private ListField fList = new ListField(){
                  protected boolean navigationClick(int status, int time) {
                      System.out.println("omt Click");
                      return true;
                  };
              };
          

          【讨论】:

            【解决方案5】:

            您可以通过覆盖来检测对每个列表项的点击

            protected boolean navigationClick(int status,int time)
            

            然后,您只需要弄清楚如何响应点击。我这样做的方法是使用为每个列表项设置的匿名类。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-01-07
              • 1970-01-01
              相关资源
              最近更新 更多