2011-10-11 198 views
0

我已成功提取聯繫人列表。但我無法添加該列表的複選框。我已經從複選框和其工作單獨的程序。但不包括聯繫人列表。有人可以告訴我,我應該在哪裏添加複選框?以下是代碼:blackberry:在列表中添加複選框

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

ListField不是爲此設計的。它的列表項不是Manager,所以你不能添加任何子字段。換句話說,這對於BB上的ListField是不可能的。 ListField是一種在UI上呈現長列表而不會吃太多RAM的方式(因爲在這種情況下只有唯一的UI對象 - ListField)。

如果您的清單不太長(10 - 20項),請考慮使用VerticalFieldManager而不是ListField。如果列表很長& &你真的需要複選框,然後考慮使用VerticalFieldManager +列表分頁。