LWUIT 1.5如何捕获标签点击事件,从服务器获取内容并在选定的标签中显示?

时间:2012-03-08 10:56:39

标签: java-me lwuit lwuit-tabs

我正在使用LWUIT 1.5标签来显示通知。我有三个Tabs,我从php Web服务获取通知。我成功获取了第一个Tab的通知列表。但对于接下来的两个Tabs,我无法理解我应该写什么代码

  1. 检测到点击了第二个/第三个Tab。我知道如何将commandListener添加到ButtoncommandListener选择的Tab是什么?
  2. 如何从服务器收到新数据时刷新Tab的内容?

    private void showNotificationList(){

    try {
    
        Form f = new Form("Notifications");
        f.setScrollable(false);
        f.setLayout(new BorderLayout());
        final List list = new List(getNotifications()); //gets hashtables - notices
        list.setRenderer(new GenericListCellRenderer(createGenericRendererContainer(), createGenericRendererContainer()));
        list.setSmoothScrolling(true);
        //System.out.println("adding list component to listview");
        list.addActionListener(new ActionListener() {
    
            public void actionPerformed(ActionEvent evt) {
                int i = list.getSelectedIndex();
                noticeDetailsForm(notices[i]);
                //Dialog. show( "title",  notices[i].toString(),  "ok",  "exitt");
            }
        });
        //Container c2 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        //c2.addComponent(list);
        Tabs tabs = new Tabs(Tabs.TOP);
        tabs.addTab("Recent", list);
        tabs.addTab("Urgent", new Label("urgent goes here"));
        tabs.addTab("Favourites", new Label("favs goes here"));
    
        //f.addComponent(tabs);
        f.addComponent(BorderLayout.CENTER, tabs);
        Command backComm = new Command("Back") {
    
            public void actionPerformed(ActionEvent ev) {
                Dashboard.dbInstance.setUpDashboard();
    
            }
        };
        f.addCommand(backComm);
        //System.out.println("showing lsit form");
        f.show();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    

    }

    private Container createGenericRendererContainer() throws IOException { //System.out.println("container called");
    
    //System.out.println("container called");
    Container c = new Container(new BorderLayout());
    c.setUIID("ListRenderer");
    
    Label xname = new Label("");
    Label description = new Label();
    Label focus = new Label("");
    Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    xname.setName("Name");
    xname.getStyle().setBgTransparency(0);
    xname.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
    //description.setFocusable(true);
    description.setName("Description");
    cnt.addComponent(xname);
    cnt.addComponent(description);
    c.addComponent(BorderLayout.CENTER, cnt);
    Button thumb = new Button(Image.createImage("/res/home-work.png"));
    //Image img = Image.createImage("/res/home-work.png");
    c.addComponent(BorderLayout.WEST, thumb);
    
    return c;
    

    }

    private Hashtable [] getNotifications(){     int total = notices.length;     //System.out.println(total);     Hashtable [] data = new Hashtable [total];     // Hashtable [] data = new Hashtable [5];     / data [0] = new Hashtable();     data [0] .put(“Name”,“Shai”);     data [0] .put(“Surname”,“Almog”);     data [0] .put(“Selected”,Boolean.TRUE); /

    for (int i = 0; i < total; i++) {
        data[i] = new Hashtable();
        //System.out.println(notices[i].getName());
        data[i].put("Name", notices[i].getName());
        data[i].put("Description", notices[i].getDescription());
        data[i].put("Id", Integer.toString(notices[i].getId()));
    }
    
    return data;
    

    }

1 个答案:

答案 0 :(得分:2)

1)我遇到了同样的问题,我通过覆盖Keyreleased的{​​{1}}而不是Form解决了这个问题 在其中我检查了焦点的组件,如果是Tab获取“tab.selectedIndex”来检测我Tab我是谁并加载适当的数据。 这是示例代码(这在我的派生形式中扩展了Form)

**

Tab

2)关于刷新数据我做了一个解决方案,我不知道它是否正确 我获取新数据,创建新的public void keyReleased(int keyCode) { Component p=this.getFocused(); String str= p.getClass().getName(); if(str.toLowerCase().indexOf("radiobutton")!=-1){ // Radiobutton because when u Here do tab specific work focus on the tab it returns radiobutton. lwuit understands tabs as list of radiobuttons }** 并删除旧数据然后附加新数据 致电List;