ListCellRenderer on a list

19 views
Skip to first unread message

giuli....@gmail.com

unread,
Oct 26, 2015, 5:17:27 AM10/26/15
to CodenameOne Discussions
Hello,
i am doing a List of hours, so inside every cell i set the hourLabel and i need to write 3 arrayList of different activities(each activity is a button that i click and then go to another View).
My Layout in the constructor is a TableLayout(1,2), in the first column there is the hourLabel, and in the second column i got the Container for the activities. 
So, in my getListCellRendererComponent function i set the label, i got an ArrayList value associated to the hour, so i add a button to my activity container for every activity in the value. 
The problem is: i got 9 activities on hour 10, but my listrenderer draws 9 activities for every Hour! Maybe because i'm doing an addComponent to the same Container?

I managed to solve the problem putting a activityContainer.removeAll() at the start of my cellrenderer, so the output is correct but the buttons loose the action listener, i can't click them.

The code is like this:

public Component getListCellRendererComponent(List list, ArrayList<Attivita> value, int index, boolean isSelected) {
        attivita.removeAll();
        
        hour.setText(String.valueOf(index));
        
        for (int i=0; i < value.size(); i++) {
            final Attivita v = value.get(i);
            if(v != null) {
                if(v instanceof Chiamata) {
                    button = new Button("Ch");
                    button.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            Dialog.show("Alert", v.getDescrizione(), "Ok", null);
                        }
                    });
                    //button.setUIID("Label");
                    attivita.addComponent(button);
                    
                }

                if(v instanceof Appuntamento) {
                    button = new Button("A");
                    button.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            Dialog.show("Alert", v.getDescrizione(), "Ok", null);
                        }
                    });
                    //button.setUIID("Label");
                    attivita.addComponent(button);
                    
                }

                if(v instanceof Compito) {
                    button = new Button("Co");
                    button.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            Dialog.show("Alert", v.getDescrizione(), "Ok", null);
                        }
                    });
                    //button.setUIID("Label");
                    attivita.addComponent(button);
                    
                }
            }
            
        }
        
        return this;
    }


Shai Almog

unread,
Oct 27, 2015, 12:35:17 AM10/27/15
to CodenameOne Discussions, giuli....@gmail.com
Hi,
a list wouldn't be a good solution here since a list hides a lot of the details here. A renderer is stateless so the buttons won't fire the right events and its pretty much a pain.
I'd just use a Container with the buttons which would also perform better probably:
http://www.codenameone.com/blog/deeper-in-the-renderer.html

You can also use the time version of the Picker component which will translate compile to native iOS/Android widgets.

giuli....@gmail.com

unread,
Oct 27, 2015, 4:02:24 AM10/27/15
to CodenameOne Discussions, giuli....@gmail.com
Ok, thank you for the reply! I will try a Container but i have another problem: when i enter in the View, i want to autoscroll on the current hour, with the list was easy(with setSelectedIndex()), but how can i do the same with a container with a Vertical Box Layout containing a Table Layout (1,2) that has in the first column the hour and in the second column a Container with FlowLayout that got the Activities?
Thank you

Shai Almog

unread,
Oct 27, 2015, 11:42:05 PM10/27/15
to CodenameOne Discussions, giuli....@gmail.com
Component.requestFocus() will automatically scroll to that component.

giuli....@gmail.com

unread,
Oct 28, 2015, 10:38:13 AM10/28/15
to CodenameOne Discussions, giuli....@gmail.com
It doesn't work, i tried it using my hourContainer and also on my hourLabel. Do i need other instructions? 

Shai Almog

unread,
Oct 28, 2015, 11:44:57 PM10/28/15
to CodenameOne Discussions, giuli....@gmail.com
Did you do it after the form was shown?
Is the UI normally scrollable?
Reply all
Reply to author
Forward
0 new messages