2013-04-05 3 views
-1

장치에 저장된 모든 연락처 목록을 그리려합니다. 모든 것이 좋지만 모든 연락처를 선택할 때 화면에 그려지는 연락처 만받습니다. 즉, 화면에 표시되는 연락처 만 나열합니다. 나머지 연락처를 얻으려면 목록을 스크롤해야합니다.Blackberry에서 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는 점을 있고 난 귀하의 코드를 사용합니다. 잘 작동하고 내 문제를 해결했습니다. 많은 많은 감사 ... –

+0

많은, 많은 * 환영 * :) – Nate