在代号一中加载联系人的最快和最可靠的方法

Fastest and most reliable way to load contacts in Codename one

我正在尝试将所有 phone 联系人作为注册的一部分添加到我的应用程序中,并且我希望这些联系人加载图片(不可协商)。

我使用下面的代码在 iOS 上使应用程序崩溃并且在 android 上非常慢。加载 Codename one 中所有 phone 联系人的最佳方式是什么?

其他应用程序设法在很短的时间内获取我的所有联系人,所以我相信这可以做到。为什么代码在 iOS 上使我的应用程序崩溃?

Image defaultIcon = fontIcon("\ue113", 7, 0xbcbcbc);
try {
    InfiniteProgress progress = new InfiniteProgress();
    progress.setAnimation(fontIcon(FontIcon.FONTICON_SPIN6 + "", 4, 0x12a4f4));
    Dialog ipDlg = progress.showInifiniteBlocking();
    final String[] myContacts = Display.getInstance().getAllContacts(true);
    for (final String contactId : myContacts) {
        Contact contact = Display.getInstance().getContactById(contactId, true, true, true, true, true);
        Hashtable numbers = contact.getPhoneNumbers();
        Enumeration nums = numbers.elements();

        String firstName = contact.getFirstName() != null ? contact.getFirstName() : "";
        String familyName = contact.getFamilyName() != null ? contact.getFamilyName() : "";
        String names = firstName + " " + familyName;
        while (nums.hasMoreElements()) {
            String phoneNumber = (String) nums.nextElement();
            MultiButton multiContact = new MultiButton(names);
            multiContact.setTextLine2(phoneNumber);
            Image img = contact.getPhoto();
            multiContact.setIcon(img != null ? img.scaledWidth(Size(7)) : defaultIcon);
            content.add(multiContact);
        }
    }
    ipDlg.dispose();
} catch (Exception ex) {
}

这是加载联系人的最快方式:

Contact[] contacts = Display.getInstance().getAllContacts(true, false, false, false, false, false);

加载联系人后,您可以根据要求懒惰地加载图像。例如:

new Thread(() -> { 
    Contact[] cnts = Display.getInstance().getAllContacts(true, false, true, false, false, false); 
    for (int i = 0; i < cnts.length; i++) { 
        Contact cnt = cnts[i]; 
        Image pic = cnt.getPhoto(); 
        if(pic != null) { 
           contactsPics.put(cnt.getId(), pic); 
        } 
    } 
}).start();