【问题标题】:Fastest and most reliable way to load contacts in Codename one在代号一中加载联系人的最快和最可靠的方法
【发布时间】:2016-02-16 08:07:39
【问题描述】:

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

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

其他应用程序可以在很短的时间内获取我的所有联系人,所以我确信这可以做到。为什么代码会在 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) {
}

【问题讨论】:

    标签: java codenameone


    【解决方案1】:

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

    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();
    

    【讨论】:

    • 谢谢陈,请问如何懒加载图片并缓存呢?因为我仍然需要遍历所有联系人。
    • new Thread(() -> { Contact[] cnts = Display.getInstance().getAllContacts(true, false, true, false, false, false); for (int i = 0; i
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2016-12-19
    • 2014-01-31
    • 1970-01-01
    相关资源
    最近更新 更多