在Blackberry中绘制有限的ListField项而不是完整列表

时间:2013-04-05 06:00:27

标签: blackberry blackberry-jde

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

这是我的代码:

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

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

1 个答案:

答案 0 :(得分: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;
}