views:

218

answers:

2

How can we get Field Labels from PIMItem. The following code is with PIMList

String label = pimList.getAttributeLabel(
    blackBerryContact.getAttributes(Contact.TEL, i));

But i have PIMItem. There is a method PIMItem.getPIMList() which returns null for me in the code below. THE API at http://www.blackberry.com/developers/docs/5.0.0api/index.html says "getPIMList() Gets the PIMList associated with this item." Below is sample code that i am trying to achive -

// Load the address Book and allow the user to select a contact
BlackBerryContactList contactList = (BlackBerryContactList) 
    PIM.getInstance().openPIMList(PIM.CONTACT_LIST,PIM.READ_ONLY);
PIMItem userSelectedContact = contactList.choose();
// Now get the Field labels for contact numbers for userSelectedContact 
A: 
class Scr extends MainScreen {
    Scr() {

    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(new MenuItem("add label", 1, 1){public void run() {
            try {
                BlackBerryContactList contactList = 
                    (BlackBerryContactList) PIM.getInstance().openPIMList(
                    PIM.CONTACT_LIST, PIM.READ_ONLY);
                BlackBerryContact contact = 
                    (BlackBerryContact) contactList.choose();
                    add(new LabelField(getContactInfo(contact)));
            } catch (PIMException e) {
                e.printStackTrace();
            }

        }});
    }

    String getContactInfo(BlackBerryContact c) {
        StringBuffer result = new StringBuffer();
        result.append("Name: ");
        result.append(c.getStringArray(
                BlackBerryContact.NAME, 0)[BlackBerryContact.NAME_GIVEN]);
        result.append(" ");
        result.append(c.getStringArray(
                BlackBerryContact.NAME, 0)[BlackBerryContact.NAME_FAMILY]);
        result.append("Email: ");
        result.append("\n");
        result.append(c.getString(
                BlackBerryContact.EMAIL, BlackBerryContact.ATTR_NONE));
        return result.toString();
    }
}
Max Gontar
Max it was not possible to put all that explanation and the sample code that is working for me in this Comment Box so i have added a reply to my question. Thank you once again for your response and a new logic
Taha
+1  A: 

Thanks Max for the response. The returning NULL issue was problem with my code which i have rectified. I was also able to get Labels for Fields, but the loop retrieves only fields that the Contact has on his card.

I am looking to get all the 8 labels that Contact.TEL has -

Int maxAllowed = contactList.maxValues(Contact.TEL); // Gives me 8 

All the 8 Labels might not be in use in for a user, For e.g a user might have WORK, WORK2, HOME, HOME2 and MOBILE. Others FAX, PAGER and OTHER might not be filled i want to get all the allowed labels and update a given number for the one that is empty. How can we check and update the following

Contact.ATTR_PAGER, Contact.ATTR_FAX, Contact.ATTR_OTHER

Please let me know if the explanation is not clear, or some more details are required.

BlackBerryContactList contactList = (BlackBerryContactList) 
    PIM.getInstance().openPIMList(PIM.CONTACT_LIST,PIM.READ_WRITE);
PIMItem pimItem = contactList.choose();
BlackBerryContact blackBerryContact = (BlackBerryContact)pimItem;
PIMList pimList = blackBerryContact.getPIMList();
// To get Labels 
int phoneCount = blackBerryContact.countValues(BlackBerryContact.TEL);
String[] phoneNumbers = new String[phoneCount];
String[] labels = new String[phoneCount];
for (int i = 0; i > phoneCount; i++) {
    String phoneNumber = blackBerryContact.getString(Contact.TEL, i);
    String label = pimList.getAttributeLabel(
        blackBerryContact.getAttributes(Contact.TEL, i));
//Add the number and label to the array.
    phoneNumbers[i] = phoneNumber;
    labels[i] = label + ":" + phoneNumber;
}
Taha
Congratulations :)
Max Gontar
Hi Max Is it possible to update Contact.ATTR_PAGER, Contact.ATTR_FAX, Contact.ATTR_OTHER numbers for a contact
Taha