如何在Blackberry的联系人列表中选择All All选项菜单?

时间:2013-03-29 10:24:01

标签: blackberry blackberry-simulator blackberry-eclipse-plugin blackberry-jde

我正在尝试在我的应用程序中创建设备联系人列表,我必须在其中提供全选复选框。在检查时,它应该选择所有联系人并创建所有联系人的一个字符串缓冲区,以便我可以使用它来发送邮件到所有选择。

直到现在我可以选择一个复选框,但无法创建全选菜单。我正在附加我正在使用的代码。

public class CheckboxListField extends VerticalFieldManager implements ListFieldCallback, FieldChangeListener {
    private static String [] selectedContacts = new String[]{};
    private ChecklistData[] mListData = new ChecklistData[] {};
    private ListField mListField;
    private Vector mContacts;
    private SendEmail sendEmail;
    private int i;
    public static StringBuffer emailString = new StringBuffer();
    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(checkboxField.getContactNumber())
                    .append("\n");
                }
            }

        }
    };
    private ButtonField _inviteButton;

    // 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,String telNumber , boolean checked) {
            _stringVal = stringVal;
            _checked = checked;
            _telNumber = telNumber;

        }

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

        private boolean isChecked() {
            return _checked;
        }

        //Contact Info
        private String getContactNumber() {
            return _stringVal;
        }



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

    public CheckboxListField() {
        //INVITE BUTTON

        _inviteButton=new ButtonField("Invite Friend");
        _inviteButton.setChangeListener(this);
        _inviteButton.setMargin(2,0,10,0);

        // 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(new NullField(NullField.FOCUSABLE));
        add(_inviteButton);
        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);
                String telContact = getContact(item);
                mListData[i] = new ChecklistData(
                        displayName,telContact , 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());

            String email= contact.getString(Contact.EMAIL, 0);

            emailString = emailString.append(email+",");
        }
    }

    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 (list.getSelectedIndex() != index) {
            graphics.setBackgroundColor(index % 2 == 0 ? Color.WHITE
                    : Color.LIGHTGRAY);
            graphics.clear();
            //list.setFocus();
        }

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

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

    //GET EMAIL IDS FROM CONTACT
    public static String getContact(Contact contact){
        if (contact == null) {
            return null;
        }
        String telNumber=null;

        telNumber = contact.getString(Contact.TEL, 0);
        telNumber = "[ "+telNumber+" ]";
        System.out.println(telNumber);

        return telNumber;
    }

    public void fieldChanged(Field field, int context) {
        ChecklistData checklistData = new ChecklistData("","", false);
        if(field==_inviteButton){
            if(!checklistData.toggleChecked()){
                UiApplication.getApplication();
                synchronized (Application.getEventLock()){
                    UiApplication.getUiApplication().pushScreen(new InviteFriendEmailScreen());
                }
            }else{
                //emailId = contact.getString(Contact.EMAIL, 0);

                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(emailString.toString(),Constant.emailSubject);
                emailString=null;
            }

        }
    }

}

请建议我并帮助我创建这个特别的。

此处,在邀请按钮下方,在创建列表之前,我需要包含文字全选的复选框。 enter image description here

0 个答案:

没有答案