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

时间:2011-10-11 06:16:08

标签: blackberry java-me

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

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;
    }
}

1 个答案:

答案 0 :(得分:1)

ListField不适用于此。其列表项不是Manager,因此您无法向其添加任何子字段。换句话说,BB上的ListField无法做到这一点。 ListField是一种在UI上表示长列表而不占用太多RAM的方法(因为在这种情况下,只有UI对象 - ListField)。

如果您的列表不太长(10 - 20项),请考虑使用VerticalFieldManager代替ListField。如果列表很长&amp;&amp;你真的需要它上面的复选框,然后考虑使用VerticalFieldManager + list pagination。