LWUIT 1.5 How to capture tab click event, fetch content from server and show it in selected tab?

828 views Asked by At

I am using LWUIT 1.5 tabs to show notifications. I have three Tabs and I fetch notifications from a php web service. I successfully fetched list of notifications for first Tab. But for next two Tabs I am failing to understand what code I should write to

  1. Detect that second/third Tab is clicked. I know how to add commandListener to a Button. What commandListener is there for Tab selection?
  2. How to refresh content of a Tab when new data is received from the server?

    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

There are 1 answers

0
eman refai On

1)I had the same problem and I solved it by overriding Keyreleased of the Form not the Tab and inside it I check for the component that is focused and if it is the Tab get "tab.selectedIndex" to detect in which Tab I am and load appropriate data . Here is Sample code(this inside the my derived form that extends Form )

**

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 
                      }**

2)and about refreshing the data I did a solution and I don't Know if its right I get the new data , create new List and remove the old one and attach the new one then call Form.repaint();