2013-04-05 115 views
-1

我想绘制一个设备中保存的所有联系人列表。一切都很好,但是当我选择所有联系人时,我只能看到在屏幕上绘制的联系人。换句话说,列出仅绘制屏幕上可见的联系人。要获取剩余的联系人,我必须滚动列表。有限的ListField项目被绘制而不是完整的黑莓列表

这里是我的代码:

public class CheckboxListField extends VerticalFieldManager implements ListFieldCallback, FieldChangeListener { 
    private static Vector selectedContacts ; 
    private ChecklistData[] mListData = new ChecklistData[] {}; 
    private ListField mListField; 
    private static Vector mContacts; 
    private ContactList contactList; 
    private Enumeration allContacts; 
    private SendEmail sendEmail; 
    private boolean isChecked=false; 
    private BlackBerryContact contactItem; 
    private VerticalFieldManager _mainVFM = new VerticalFieldManager(); 
    private int i; 
    private int j=0; 
    private String emails=""; 
    private ButtonField _inviteButton; 
    private HorizontalFieldManager selectAllHFM; 
    private CustomButtonField selectAllButton; 
    private Bitmap _uncheckBmp; 
    private Bitmap _checkBmp; 
    private LabelField selectAllLabel; 
    private CheckboxField selectAllCheckBox; 
    private VerticalFieldManager contactListVFM; 
    private boolean listItemChecked=false; 
    private StringBuffer rowString; 
    private boolean getCBoxStatus; 

    // 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; 
     private String _telNumber; 

     ChecklistData(String stringVal, boolean checked) { 
      _stringVal = stringVal; 
      _checked = checked; 
      //_telNumber = telNumber; 

     } 

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

     private boolean isChecked() { 
      return _checked; 
     } 


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


    public CheckboxListField() { 

     _mainVFM.add(createContactList(isChecked)); 
     add(_mainVFM); 

    } 

    public VerticalFieldManager createContactList(boolean checked){ 
     isChecked = checked; 
     selectedContacts = new Vector(); 

     //INVITE BUTTON 
     contactListVFM = new VerticalFieldManager(); 
     _inviteButton=new ButtonField("Invite Friend"); 
     _inviteButton.setChangeListener(this); 
     _inviteButton.setMargin(2,0,10,0); 

     //SELECT ALL CHECKBOX 

     selectAllHFM = new HorizontalFieldManager(); 
     _uncheckBmp = Bitmap.getBitmapResource("Uncheck.png"); 
     _checkBmp = Bitmap.getBitmapResource("checked.png"); 
     selectAllButton = new CustomButtonField(29, "", _uncheckBmp, _checkBmp, ButtonField.CONSUME_CLICK); 
     selectAllButton.setChangeListener(this); 
     selectAllButton.setMargin(5,5,5,5); 

     selectAllCheckBox = new CheckboxField("Select All", isChecked){ 
      protected boolean navigationClick(int status, 
        int time) { 
       selectedContacts = new Vector(); 
       emails = ""; 
       boolean getCBoxStatus = selectAllCheckBox.getChecked(); 

       if(listItemChecked == false){ 
        if(_mainVFM.getFieldCount()!= 0){ 
         _mainVFM.deleteAll(); 
         _mainVFM.add(createContactList(getCBoxStatus)); 
        } 
       } 

       return true; 
      } 
     }; 
     selectAllCheckBox.setChangeListener(this); 

     selectAllLabel = new LabelField("Select All"); 
     selectAllLabel.setMargin(5,5,5,5); 

     selectAllHFM.add(selectAllCheckBox); 
     //selectAllHFM.add(selectAllLabel); 


     // 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); 
     //contactListVFM.add(new NullField(NullField.FOCUSABLE)); 
     contactListVFM.add(_inviteButton); 
     contactListVFM.add(selectAllHFM); 
     contactListVFM.add(new SeparatorField()); 
     contactListVFM.add(mListField); 

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

     return contactListVFM; 
    } 


    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++) { 
       contactItem = (BlackBerryContact) mContacts 
       .elementAt(i); 

       String displayName = getDisplayName(contactItem); 

       // String telContact = getContact(item); 
       mListData[i] = new ChecklistData(
         displayName, isChecked); 
       mListField.invalidate(i); 
       System.out.println(">>>>>>>>>"+mListData[i]); 
      } 
      mListField.setSize(size); 
      //invalidate(); 
     } 
    }; 




    protected void toggleItem() { 
     listItemChecked = true ; 
     selectAllCheckBox.setChecked(false); 

     listItemChecked =false ; 
     // Get the index of the selected row. 
     int index = mListField.getSelectedIndex(); 
     System.out.println("..............."+index); 

     if (index != -1) { 
      // Get the ChecklistData for this row. 
      ChecklistData data = mListData[index]; 

      // Toggle its status. 
      data.toggleChecked(); 
      mListField.invalidate(index); 

     } 
    } 



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

      allContacts = contactList.items(); 
      mContacts = enumToVector(allContacts); 
      mListField.setSize(mContacts.size()); 
      System.out.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>>>>>>>>>>"+mListField.getSize()); 
      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()){ 
      Contact contact = (Contact) allContacts.nextElement(); 
      if(contactList.isSupportedField(Contact.EMAIL)&& (contact.countValues(Contact.EMAIL) > 0)) { 
       String emailID=contact.getString(Contact.EMAIL, 0); 
       if(emailID.length() !=0 && emailID != null){ 
        v.addElement(contact); 
       } 
      } 
     } 
     return v; 
    } 


    public void drawListRow(ListField list, 
      Graphics graphics, int index, int y, int w) { 
     rowString = new StringBuffer(); 
     Object obj = this.get(list, index); 
     if (list.getSelectedIndex() != index) { 
      graphics.setBackgroundColor(index % 2 == 0 ||index==0 ? Color.WHITE 
        : Color.LIGHTGRAY); 
      graphics.clear(); 
      //list.setFocus(); 
     } 

     BlackBerryContact contact = (BlackBerryContact) mContacts 
     .elementAt(index); 

     String email= contact.getString(Contact.EMAIL, 0); 
     int vecIndex = selectedContacts.indexOf(email); 


     if (obj != null) { 
      ChecklistData currentRow = (ChecklistData) obj; 


      if (currentRow.isChecked()) { 
       if(vecIndex == -1){ 
        selectedContacts.addElement(email); 
       } 
       rowString 
       .append(Characters.BALLOT_BOX_WITH_CHECK); 
      } else { 
       selectedContacts.removeElement(email); 
       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); 

    } 

    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]; 
     } 
     System.out.println(",,,,,,,,,,,,,,,,,,,,,,,"+mListData.length); 
     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(); 
    } 


    public void fieldChanged(Field field, int context) { 

     if(field==_inviteButton){ 

      for(int n=0 ; n<selectedContacts.size() ; n++){ 

       emails= emails + selectedContacts.elementAt(n)+","; 

      } 
      //} 
      String mailBody =": "+Jxa.loginUserName+" invited you on NaijaPings app. Please download NaijaPings Android app from here "+"http://appworld.blackberry.com/webstore/content/77264/?lang=en" ; 
      sendEmail=new SendEmail(mailBody); 
      sendEmail.Email(emails,Constant.emailSubject); 
      emails ="" ; 
      selectedContacts.removeAllElements(); 


     }else if(field == selectAllCheckBox){ 

      selectedContacts = new Vector(); 
      emails = ""; 
      getCBoxStatus = selectAllCheckBox.getChecked(); 
      //selectedContacts.removeAllElements(); 

      if(listItemChecked == false){ 
       if(_mainVFM.getFieldCount()!= 0){ 
        _mainVFM.deleteAll(); 
        _mainVFM.add(createContactList(getCBoxStatus)); 
       } 
      } 

     } 
    } 

} 

这里,drawListRow()的get()方法被调用只是很多时候是接触次数在屏幕上是可见的。要添加其他联系人,我必须滚动列表。

drawListRow()方法,我将这些联系人为selectedContacts载体,比使用这些向量来获得联系,发送邮件。只有在绘制特定列表项时才会添加联系人。

那么,我怎样才能得到所有选定的联系人而无需滚动列表?

回答

2

这与problem you had in one of your other recent questions相似。问题是,drawListRow()是一个回调,旨在让你绘制需要绘制的行。这并不意味着要做其他事情,比如将联系人列表汇编成电子邮件。

BlackBerry OS试图保持高效,所以它只会要求您向drawListRow()提供实际上可见给用户(屏幕上)的行。更多的东西将是浪费。

所以,如果你想组装一个所有选定行的列表,你应该在别的地方做,而不是在drawListRow()

它看起来对我来说,你可以通过使用此代码,无论你想建立所有当前选择的行的列表:

public Vector getSelectedContacts() { 

    selectedContacts.removeAllElements(); 

    for (int i = 0; i < mListData.length; i++) { 
     Object obj = mListData[i]; 
     if (obj != null) { 

      BlackBerryContact contact = (BlackBerryContact) mContacts.elementAt(i); 
      String email = contact.getString(Contact.EMAIL, 0); 
      int vecIndex = selectedContacts.indexOf(email); 

      ChecklistData currentRow = (ChecklistData) obj; 

      if (currentRow.isChecked()) { 
       if(vecIndex == -1){ 
        selectedContacts.addElement(email); 
       } 
      } else { 
       // this line is probably not actually needed, since we 
       // call removeAllElements() at the start of this method 
       selectedContacts.removeElement(email); 
      } 
     } 
    } 

    return selectedContacts; 
} 
+0

内特:非常感谢Nate..I得到了点,我使用你的code.Its工作正常,解决了我的问题。很多很多,谢谢... – 2013-04-05 10:53:21

+0

很多,很多*欢迎* :) – Nate 2013-04-05 10:56:43