黑莓:在列表中添加复选框

发布于 2024-12-09 02:02:39 字数 3158 浏览 0 评论 0原文

我已成功获取联系人列表。但我无法在该列表中添加复选框。我已经从复选框及其工作中制作了单独的程序。但不包括联系人列表。有人可以告诉我应该在哪里添加复选框吗?这是代码:

public final class ContactsScreen extends MainScreen implements ListFieldCallback {
    private ListField listField;
    private ContactList blackBerryContactList;
    private Vector blackBerryContacts;

    public ContactsScreen(){
        CheckboxField checkBox1 = new CheckboxField();

        setTitle(new LabelField( "Contacts", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ));

        listField = new ListField();
        listField.setCallback(this);
        add(listField);
        add(new RichTextField("Size" +(listField)));
        reloadContactList();
    }

    private boolean reloadContactList() {
        try {
            blackBerryContactList = (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
            Enumeration allContacts = blackBerryContactList.items();
            blackBerryContacts = enumToVector(allContacts);
            listField.setSize(blackBerryContacts.size());
            return true;
        }
        catch(PIMException e){
            return false;
        }
    }

    private Vector enumToVector(Enumeration contactEnum) {
        Vector v = new Vector();

        if (contactEnum == null)
            return v;

        while (contactEnum.hasMoreElements()) {
            v.addElement(contactEnum.nextElement());
        }
        return v;
    }

    public void drawListRow(ListField fieldVar, Graphics graphics, int index, int y, int width){
        if ( listField == fieldVar && index < blackBerryContacts.size())
        {
            add(new RichTextField(blackBerryContacts.size()));
            BlackBerryContact item = (BlackBerryContact)blackBerryContacts.elementAt(index);
            String displayName = getDisplayName(item);
            graphics.drawText(displayName, 0, y, 0, width);
        }
    }

    public Object get(ListField fieldVar, int index)
    {
        if (listField == fieldVar) {
            return blackBerryContacts.elementAt(index);
        }
        return null;
    }

    public int getPreferredWidth(ListField fieldVar ) {
        return Display.getWidth();
    }

    public int indexOfList(ListField fieldVar, String prefix, int start)
    {
        return -1; // not implemented
    }

    public static String getDisplayName(Contact contact) {
        if (contact == null) {
            return null;    }

        String displayName = null;

        // First, see if there is a meaningful name set for the contact.
        if (contact.countValues(Contact.NAME) > 0) {
            final String[] name = contact.getStringArray(Contact.NAME, 0);
            final String firstName = name[Contact.NAME_GIVEN];
            final String lastName = name[Contact.NAME_FAMILY];
            if (firstName != null && lastName != null) {
                displayName = firstName + " " + lastName;
            } else if (firstName != null) {
                displayName = firstName;
            } else if (lastName != null) {
                displayName = lastName;
            }
       } return displayName;
    }
}

I have fetched contact list successfully. But I am not able to add check boxes with that list. I have made separate program from checkbox and its working. but not with the contact list. Can anybody tell me here where should I add checkboxes? Here is the code:

public final class ContactsScreen extends MainScreen implements ListFieldCallback {
    private ListField listField;
    private ContactList blackBerryContactList;
    private Vector blackBerryContacts;

    public ContactsScreen(){
        CheckboxField checkBox1 = new CheckboxField();

        setTitle(new LabelField( "Contacts", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ));

        listField = new ListField();
        listField.setCallback(this);
        add(listField);
        add(new RichTextField("Size" +(listField)));
        reloadContactList();
    }

    private boolean reloadContactList() {
        try {
            blackBerryContactList = (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
            Enumeration allContacts = blackBerryContactList.items();
            blackBerryContacts = enumToVector(allContacts);
            listField.setSize(blackBerryContacts.size());
            return true;
        }
        catch(PIMException e){
            return false;
        }
    }

    private Vector enumToVector(Enumeration contactEnum) {
        Vector v = new Vector();

        if (contactEnum == null)
            return v;

        while (contactEnum.hasMoreElements()) {
            v.addElement(contactEnum.nextElement());
        }
        return v;
    }

    public void drawListRow(ListField fieldVar, Graphics graphics, int index, int y, int width){
        if ( listField == fieldVar && index < blackBerryContacts.size())
        {
            add(new RichTextField(blackBerryContacts.size()));
            BlackBerryContact item = (BlackBerryContact)blackBerryContacts.elementAt(index);
            String displayName = getDisplayName(item);
            graphics.drawText(displayName, 0, y, 0, width);
        }
    }

    public Object get(ListField fieldVar, int index)
    {
        if (listField == fieldVar) {
            return blackBerryContacts.elementAt(index);
        }
        return null;
    }

    public int getPreferredWidth(ListField fieldVar ) {
        return Display.getWidth();
    }

    public int indexOfList(ListField fieldVar, String prefix, int start)
    {
        return -1; // not implemented
    }

    public static String getDisplayName(Contact contact) {
        if (contact == null) {
            return null;    }

        String displayName = null;

        // First, see if there is a meaningful name set for the contact.
        if (contact.countValues(Contact.NAME) > 0) {
            final String[] name = contact.getStringArray(Contact.NAME, 0);
            final String firstName = name[Contact.NAME_GIVEN];
            final String lastName = name[Contact.NAME_FAMILY];
            if (firstName != null && lastName != null) {
                displayName = firstName + " " + lastName;
            } else if (firstName != null) {
                displayName = firstName;
            } else if (lastName != null) {
                displayName = lastName;
            }
       } return displayName;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2024-12-16 02:02:39

ListField 不是为此设计的。它的列表项不是 Manager,因此您无法向其中添加任何子字段。换句话说,这对于 BB 上的 ListField 来说是不可能的。 ListField 是一种在 UI 上表示长列表的方法,而不会占用太多 RAM(因为在本例中只有唯一的 UI 对象 - ListField)。

如果您的列表不太长(10 - 20 项),请考虑使用 VerticalFieldManager 而不是 ListField。如果列表很长&&您确实需要复选框,然后考虑使用 VerticalFieldManager + 列表分页。

ListField is not designed for this. Its list item is not a Manager, so you can't add any child fields to it. In other words this is not possible with ListField on BB. ListField is a way to represent on UI a long list without eating too much RAM (since in this case there is the only UI object - the ListField).

If your list is not too long (10 - 20 items) then consider using VerticalFieldManager instead of ListField. If list is long && you really need check boxes on it then consider using VerticalFieldManager + list pagination.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文