2011-11-17 127 views
2

我用複選框做了列表視圖。我讀過類似的文章,很多人都建議在drawlistRow中進行修改,但沒有發生。 ü可以建議我應該在哪裏換,使之成爲多線list.The代碼片段是: 更新:我更新了我的代碼,它仍然沒有工作黑莓:多行列表查看

public class CheckboxListField extends MainScreen implements ListFieldCallback, FieldChangeListener { 
    int mCheckBoxesCount = 5; 
    private Vector _listData = new Vector(); 
    private ListField listField; 
    private ContactList blackBerryContactList; 
    private BlackBerryContact blackBerryContact; 
    private Vector blackBerryContacts; 
    private MenuItem _toggleItem; 
    ButtonField button; 
    BasicEditField mEdit; 
    CheckboxField cb; 
    CheckboxField[] chk_service; 
    HorizontalFieldManager hm4; 
    CheckboxField[] m_arrFields; 
    boolean mProgrammatic = false; 
    public static StringBuffer sbi = new StringBuffer(); 
    VerticalFieldManager checkBoxGroup = new VerticalFieldManager(); 
    LabelField task; 

    //A class to hold the Strings in the CheckBox and it's checkbox state (checked or unchecked). 
    private class ChecklistData 
    { 
     private String _stringVal; 
     private boolean _checked; 

     ChecklistData() 
     { 
      _stringVal = ""; 
      _checked = false; 
     } 

     ChecklistData(String stringVal, boolean checked) 
     { 
      _stringVal = stringVal; 
      _checked = checked; 
     } 

     //Get/set methods. 
     private String getStringVal() 
     { 
      return _stringVal; 
     } 

     private boolean isChecked() 
     { 
      return _checked; 
     } 

     private void setStringVal(String stringVal) 
     { 
      _stringVal = stringVal; 
     } 

     private void setChecked(boolean checked) 
     { 
      _checked = checked; 
     } 

     //Toggle the checked status. 
     private void toggleChecked() 
     { 
      _checked = !_checked; 
     } 
    } 

    CheckboxListField() 
    { 
     _toggleItem = new MenuItem("Change Option", 200, 10) 
     { 
      public void run() 
      { 
       //Get the index of the selected row. 
       int index = listField.getSelectedIndex(); 

       //Get the ChecklistData for this row. 
       ChecklistData data = (ChecklistData)_listData.elementAt(index); 

       //Toggle its status. 
       data.toggleChecked(); 

       //Update the Vector with the new ChecklistData. 
       _listData.setElementAt(data, index); 

       //Invalidate the modified row of the ListField. 
       listField.invalidate(index); 

       if (index != -1 && !blackBerryContacts.isEmpty()) 
       { 
        blackBerryContact = 
         (BlackBerryContact)blackBerryContacts. 
         elementAt(listField.getSelectedIndex()); 
        ContactDetailsScreen contactDetailsScreen = 
         new ContactDetailsScreen(blackBerryContact); 
        UiApplication.getUiApplication().pushScreen(contactDetailsScreen); 
       } 
      } 
     }; 
     listField = new ListField(); 
     listField.setRowHeight(getFont().getHeight() * 2); 
     listField.setCallback(this); 
     reloadContactList(); 

     //CheckboxField[] cb = new CheckboxField[blackBerryContacts.size()]; 
     for(int count = 0; count < blackBerryContacts.size(); ++count) 
     { 
      BlackBerryContact item = 
       (BlackBerryContact)blackBerryContacts.elementAt(count); 
      String displayName = getDisplayName(item); 
      CheckboxField cb = new CheckboxField(displayName, false); 
      cb.setChangeListener(this); 
      add(cb); 
      listField.insert(count); 
     } 
     blackBerryContacts.addElement(cb); 
     add(checkBoxGroup); 
    } 

    protected void makeMenu(Menu menu, int instance) 
    { 
     menu.add(new MenuItem("Get", 2, 2) { 
      public void run() { 
       for (int i = 0; i < checkBoxGroup.getFieldCount(); i++) { 
       //for(int i=0; i<blackBerryContacts.size(); i++) { 
        CheckboxField checkboxField = (CheckboxField)checkBoxGroup 
         .getField(i);   
        if (checkboxField.getChecked()) { 
         sbi.append(checkboxField.getLabel()).append("\n"); 
        } 
       } 
       Dialog.inform("Selected checkbox text::" + sbi); 
      } 
     }); 
     super.makeMenu(menu, instance); 
    }  

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

    //Convert the list of contacts from an Enumeration to a Vector 
    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 list, Graphics graphics, int index, int y, int w) 
    { 
     ChecklistData currentRow = (ChecklistData)this.get(list, index); 
     StringBuffer rowString = new StringBuffer(); 

     if (currentRow.isChecked()) 
     { 
      rowString.append(Characters.BALLOT_BOX_WITH_CHECK); 
     } 
     else 
     { 
      rowString.append(Characters.BALLOT_BOX); 
     } 

     //Append a couple spaces and the row's text. 
     rowString.append(Characters.SPACE); 
     rowString.append(Characters.SPACE); 
     rowString.append(currentRow.getStringVal()); 

     //graphics.drawText("ROW", 0, y, 0, w); 
     //String rowNumber = "one"; 
     //Draw the text. 
     graphics.drawText(rowString.toString(), 0, y, 0, w); 
     /*graphics.drawText("ROW " + rowNumber, y, 0, w); 
     graphics.drawText("ROW NAME", y, 20, w); 
     graphics.drawText("row details", y + getFont().getHeight(), 20, w); */ 
    } 

    public void drawRow(Graphics g, int x, int y, int width, int height) { 
     // Arrange the cell fields within this row manager. 
     layout(width, height); 

     // Place this row manager within its enclosing list. 
     setPosition(x, y); 

     // Apply a translating/clipping transformation to the graphics 
     // context so that this row paints in the right area. 
     g.pushRegion(getExtent()); 

     // Paint this manager's controlled fields. 
     subpaint(g); 

     g.setColor(0x00CACACA); 
     g.drawLine(0, 0, getPreferredWidth(), 0); 

     // Restore the graphics context. 
     g.popContext(); 
    } 
    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; 
       } 

       if (displayName != null) { 
        final String namePrefix = name[Contact.NAME_PREFIX]; 
        if (namePrefix != null) { 
         displayName = namePrefix + " " + displayName; 
        } 
        return displayName; 
       } 
      } 
      return displayName; 
     } 

    //Returns the object at the specified index. 
    public Object get(ListField list, int index) 
    { 
     return _listData.elementAt(index); 
     /*if (listField == list) 
     { 
      //If index is out of bounds an exception will be thrown, 
      //but that's the behaviour we want in that case. 
      //return blackBerryContacts.elementAt(index); 
      _listData = (Vector) blackBerryContacts.elementAt(index); 
      return _listData.elementAt(index); 
     } 
     return null;*/ 
    } 

    //Returns the first occurence of the given String, bbeginning the search at index, 
    //and testing for equality using the equals method. 
    public int indexOfList(ListField list, String p, int s) 
    { 
     //return listElements.getSelectedIndex(); 
     //return _listData.indexOf(p, s); 
     return -1; 
    } 

    //Returns the screen width so the list uses the entire screen width. 
    public int getPreferredWidth(ListField list) 
    { 
     return Graphics.getScreenWidth(); 
     //return Display.getWidth(); 
    } 

    public void fieldChanged(Field field, int context) { 
     boolean mProgrammatic = false; 

     if (!mProgrammatic) { 
     mProgrammatic = true; 
     CheckboxField cbField = (CheckboxField) field; 
     int index = blackBerryContacts.indexOf(cbField); 

     if (cbField.getChecked()) 
     { 
      for(int i=0;i<blackBerryContacts.size();i++) 
      { 
      Dialog.inform("Selected::" + cbField.getLabel()); 
      sbi=new StringBuffer(); 
      sbi.append(cbField.getLabel()); 

      }     
     } 

     mProgrammatic = false; 
    } 

    } 
+0

in for循環寫最後寫'listField.insert(count);' – BBdev

+0

我更新了代碼n它仍然無法工作。 Onlyn單行列表視圖即將到來 – Naina

+0

任何線索哪裏做錯了? – Naina

回答

0

該代碼可以與改進:

  1. 使用ListField代替VerticalFieldManager + CheckBoxField字段陣列(ListField得多快,100+控制可以UI減慢)
  2. 在列表數據使用簡單的陣列,而不是矢量(它的速度更快)
  3. 移動從接觸負載UI th閱讀(我們應該意識到阻塞UI線程沉重的程序,如:IO,網絡或聯繫人列表上班)

實際使用ListField兩個行有一個問題:我們必須設定相同的高度,在所有行ListField。所以每行總是會有兩行,不管我們是否使用第二行。但它比UI性能問題要好得多。

見代碼:

public class CheckboxListField extends MainScreen implements 
     ListFieldCallback { 
    private ChecklistData[] mListData = new ChecklistData[] {}; 
    private ListField mListField; 
    private Vector mContacts; 
    private MenuItem mMenuItemToggle = new MenuItem(
      "Change Option", 0, 0) { 
     public void run() { 
      toggleItem(); 
     }; 
    }; 

    private MenuItem mMenuItemGet = new MenuItem("Get", 0, 
      0) { 
     public void run() { 
      StringBuffer sbi = new StringBuffer(); 
      for (int i = 0; i < mListData.length; i++) { 
       ChecklistData checkboxField = mListData[i]; 
       if (checkboxField.isChecked()) { 
        sbi.append(checkboxField.getStringVal()) 
          .append("\n"); 
       } 
      } 
      Dialog.inform("Selected checkbox text::\n" 
        + sbi); 
     } 
    }; 

    // A class to hold the Strings in the CheckBox and it's checkbox state 
    // (checked or unchecked). 
    private class ChecklistData { 
     private String _stringVal; 
     private boolean _checked; 

     ChecklistData(String stringVal, boolean checked) { 
      _stringVal = stringVal; 
      _checked = checked; 
     } 

     // Get/set methods. 
     private String getStringVal() { 
      return _stringVal; 
     } 

     private boolean isChecked() { 
      return _checked; 
     } 

     // Toggle the checked status. 
     private void toggleChecked() { 
      _checked = !_checked; 
     } 
    } 

    CheckboxListField() { 
     // toggle list field item on navigation click 
     mListField = new ListField() { 
      protected boolean navigationClick(int status, 
        int time) { 
       toggleItem(); 
       return true; 
      }; 
     }; 
     // set two line row height 
     mListField.setRowHeight(getFont().getHeight() * 2); 
     mListField.setCallback(this); 
     add(mListField); 

     // load contacts in separate thread 
     loadContacts.run(); 
    } 

    protected Runnable loadContacts = new Runnable() { 
     public void run() { 
      reloadContactList(); 
      // fill list field control in UI event thread 
      UiApplication.getUiApplication().invokeLater(
        fillList); 
     } 
    }; 

    protected Runnable fillList = new Runnable() { 
     public void run() { 
      int size = mContacts.size(); 
      mListData = new ChecklistData[size]; 
      for (int i = 0; i < mContacts.size(); i++) { 
       BlackBerryContact item = (BlackBerryContact) mContacts 
         .elementAt(i); 
       String displayName = getDisplayName(item); 
       mListData[i] = new ChecklistData(
         displayName, false); 
      } 
      mListField.setSize(size); 
     } 
    }; 

    protected void toggleItem() { 
     // Get the index of the selected row. 
     int index = mListField.getSelectedIndex(); 
     if (index != -1) { 
      // Get the ChecklistData for this row. 
      ChecklistData data = mListData[index]; 

      // Toggle its status. 
      data.toggleChecked(); 

      // Invalidate the modified row of the ListField. 
      mListField.invalidate(index); 

      BlackBerryContact contact = (BlackBerryContact) mContacts 
        .elementAt(mListField 
          .getSelectedIndex()); 

      // process selected contact here 

     } 
    } 

    protected void makeMenu(Menu menu, int instance) { 
     menu.add(mMenuItemToggle); 
     menu.add(mMenuItemGet); 
     super.makeMenu(menu, instance); 
    } 

    private boolean reloadContactList() { 
     try { 
      ContactList contactList = (ContactList) PIM 
        .getInstance() 
        .openPIMList(PIM.CONTACT_LIST, 
          PIM.READ_ONLY); 

      Enumeration allContacts = contactList.items(); 
      mContacts = enumToVector(allContacts); 
      mListField.setSize(mContacts.size()); 
      return true; 
     } catch (PIMException e) { 
      return false; 
     } 
    } 

    // Convert the list of contacts from an Enumeration to a Vector 
    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 list, 
      Graphics graphics, int index, int y, int w) { 
     Object obj = this.get(list, index); 
     if (obj != null) { 
      ChecklistData currentRow = (ChecklistData) obj; 
      StringBuffer rowString = new StringBuffer(); 

      if (currentRow.isChecked()) { 
       rowString 
         .append(Characters.BALLOT_BOX_WITH_CHECK); 
      } else { 
       rowString.append(Characters.BALLOT_BOX); 
      } 
      // Append a couple spaces and the row's text. 
      rowString.append(Characters.SPACE); 
      rowString.append(Characters.SPACE); 
      rowString.append(currentRow.getStringVal()); 
      // Draw the text. 
      graphics.drawText(rowString.toString(), 0, y, 
        0, w); 
      String secondLine = "Lorem ipsum dolor sit amet, " 
        + "consectetur adipiscing elit."; 
      graphics.drawText(secondLine, 0, y 
        + getFont().getHeight(), 
        DrawStyle.ELLIPSIS, w); 
     } else { 
      graphics.drawText("No rows available.", 0, y, 
        0, w); 
     } 
    } 

    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; 
      } 
      if (displayName != null) { 
       final String namePrefix = name[Contact.NAME_PREFIX]; 
       if (namePrefix != null) { 
        displayName = namePrefix + " " 
          + displayName; 
       } 
       return displayName; 
      } 
     } 
     return displayName; 
    } 

    // Returns the object at the specified index. 
    public Object get(ListField list, int index) { 
     Object result = null; 
     if (mListData.length > index) { 
      result = mListData[index]; 
     } 
     return result; 
    } 

    // Returns the first occurrence of the given String, 
    // beginning the search at index, and testing for 
    // equality using the equals method. 
    public int indexOfList(ListField list, String p, int s) { 
     return -1; 
    } 

    // Returns the screen width so the list uses the entire screen width. 
    public int getPreferredWidth(ListField list) { 
     return Graphics.getScreenWidth(); 
     // return Display.getWidth(); 
    } 
} 

blackberry chackbox two lines per row list field

有一個很好的編碼!