开发者

blackberry: adding checkboxes in a list

开发者 https://www.devze.com 2023-04-11 22:31 出处:网络
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 te

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号