-1

我正在尝试绘制保存在设备中的所有联系人的列表。一切都很好,但是当我选择所有联系人时,我只得到那些在屏幕上绘制的联系人。换句话说,仅列出那些在屏幕上可见的联系人。要获得剩余的联系人,我必须滚动列表。

这是我的代码:

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向量中,而不是使用这些向量来获取联系人以发送邮件。只有在绘制特定列表项时才会添加联系人。

那么,如何在不滚动列表的情况下获取所有选定的联系人?

4

1 回答 1

2

这类似于您在最近的其他问题中遇到的问题。问题是drawListRow()是一个回调,旨在让您绘制需要绘制的行。它并不意味着做任何其他事情,例如将联系人列表组装到电子邮件中。

黑莓操作系统试图提高效率,因此它只会要求您提供用户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;
}
于 2013-04-05T09:59:19.580 回答