Question

I am trying to draw a list of all contacts saved in device. Everything is fine but when I select all contacts, I get only those contacts which are drawn on the screen. In other words, list drawing only those contacts which are visible on screen. To get the remaining contacts I have to scroll the list.

Here is my code:

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

        }
    }

}

Here ,in drawListRow() , get() method is called only that many times that is number of contacts are visible on the screen. For remaining contact to add, I have to scroll the list.

In drawListRow() method I am adding those contacts into selectedContacts vector and than using those vector to get contact to send a mail. Contacts will be added only when particular list item will be drawn.

So, how I can get all selected contact without scrolling the list?

Was it helpful?

Solution

This is similar to the problem you had in one of your other recent questions. The problem is that drawListRow() is a callback designed to let you draw the rows that need drawing. It's not meant to do anything else, like assembling a list of contacts to email.

The BlackBerry OS tries to be efficient, so it will only ask you to drawListRow() for the rows that are actually visible to the user (on screen). Anything more would be wasteful.

So, if you want to assemble a list of all selected rows, you should do it somewhere else, not in drawListRow().

It looks to me like you can build a list of all currently selected rows by using this code, wherever you want:

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top