Creating contacts list takes long and causes app to freeze.

36 views
Skip to first unread message

propag...@gmail.com

unread,
Apr 30, 2015, 9:36:05 AM4/30/15
to codenameone...@googlegroups.com
Hi,
I've been trying to get a list of contacts in a dialog.
The idea is that the user presses a button, a list of names appear, and when the user selects a name, a textfield has the text set to the selected contact's phone number.

Using this blog entry as an example, I went ahead and used the ContactsModel, in conjunction with a ListCellRenderer. A basic of idea of my code follows:

ContactsDialog
public class ContactsDialog extends Dialog {
...
   
private void addContacts() {
       
String[] contacts = ContactsManager.getAllContactsWithNumbers();
 
       
ContactsModel model = new ContactsModel(contacts);
       
ContactsList list = new ContactsList(model, this);
 
       
this.addComponent(list);
   
}
...
}


ContactsList
public class ContactsList extends List<ContactButton> {
...
   
public ContactsList(ContactsModel model, ContactsDialog parent) {
       
super(model);
       
this.setRenderer(new ContactButton(parent));
   
}
...
}


ContactButton
public class ContactButton extends Container implements ListCellRenderer<Hashtable> {
...
   
private Label focus = new Label("");

   
public Component getListCellRendererComponent(List list, Hashtable value, int index, boolean isSelected) {
       
Contact contact = (Contact) value.get(CONTACT);
       
String displayName = (String) value.get(DISPLAY_NAME);
       
String phoneNumber = (String) value.get(PHONE_NUMBER);
       
if (contact != null) {
           
if (isSelected) {
                requestFocus
();
           
}
            build
(displayName, phoneNumber);
       
}
       
return this;
   
}


   
public Component getListFocusComponent(List list) {
        focus
.getStyle().setBgTransparency(100);
       
return focus;
   
}
...
}

On the emulator it works fine, with the 4 or 5 contacts, but when I use the test device (Galaxy Note 2), with just over 20 contacts, the list takes a long time to show and shuffle the contacts around, often even showing multiples of the same contact for brief periods of time.
Then, whenever an action is taken, the action doesn't seem to take place until after the list has been completely built, causing frustration from the user that keeps spam-tapping a contact, or the back button.
Scrolling also seems to be rather slow with this list, and closing and disposing of the dialog seems to take a long time to render.

Furthermore, via logs, I found that the getListCellRendererComponent is called multiple times for the same contact and though the list only shows it once (after its been fully built), the processing is done multiple times because of this.

I also tried simply using a boxlayout with a bunch of buttons, but this has the downside of waiting for all the contacts to be rendered before showing the dialog.

Is there something I'm doing incorrectly, or maybe inefficiently that causes the slowness?

If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans 7.3.1
Desktop OS: Windows 7
Simulator: Nexus
Device: Galaxy Note 2 (Android)

Chen Fishbein

unread,
Apr 30, 2015, 12:01:50 PM4/30/15
to codenameone...@googlegroups.com, propag...@gmail.com
Hi,
Don't preform any logic inside getListCellRendererComponent this method will be called many times it must be highly efficient.
Try to follow the contacts demo from the KitchenSink demo.


Reply all
Reply to author
Forward
0 new messages