Question

I've been trying to make multi contact picker list with checkbox. I already created Multi Contact Picker with contact having atleast one phone number in its contact.

Que

Now I've added contact with in ListView. But contact appear with single contact number in it, it's taking first phone number which is saved in Contact Application. I've added LinkedHashMap & Set to add indexer on the Contact List.

Did LinkedHashMap or Set is removing duplicate values from Collections?

How can i fetch all the phone number associated with that contact name?

Code Snippet:-

ContentResolver cr = getContentResolver();
    String selection = Data.HAS_PHONE_NUMBER + " > '" + ("0") + "'";
    
    Cursor cur = cr.query(Data.CONTENT_URI, new String[] { Data.CONTACT_ID, Data.MIMETYPE, Email.ADDRESS,
            Contacts.DISPLAY_NAME, Phone.NUMBER }, selection, null, Contacts.DISPLAY_NAME);

    
     Contact contact;
    if (cur.getCount() > 0) {

        while (cur.moveToNext()) {

            String id = cur.getString(cur.getColumnIndex(Data.CONTACT_ID));

            String mimeType = cur.getString(cur.getColumnIndex(Data.MIMETYPE));

            if (allContacts.containsKey(id)) {
                // update contact
                contact = allContacts.get(id);
            } else {
                contact = new Contact();
                allContacts.put(id, contact);
                // set photoUri
                contact.setContactPhotoUri(getContactPhotoUri(Long.parseLong(id)));
            }

            if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE))
                // set name
                contact.setContactName(cur.getString(cur.getColumnIndex(Contacts.DISPLAY_NAME)));

            if (mimeType.equals(Phone.CONTENT_ITEM_TYPE))
                // set phone munber
                contact.setContactNumber(cur.getString(cur.getColumnIndex(Phone.NUMBER)));
        }
    }

    cur.close();
    // get contacts from hashmap
    contacts.clear();
    contacts.addAll(allContacts.values());
Was it helpful?

Solution

These is just a temporary fix as of now, I've been trying to add all contact number associated with same contact person inside android.provider.ContactsContract.

I've added all contact number associated with same contact person inside beans by appending with delimiter(",") which i can fetch back using Tokenizer. But no idea to know which kinda contact type it is whether HOME, WORK, OFFICE , it's less important to me as of now.

Fix:

if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)){
// set phone number
    if (contact.getContactNumber().toString().length() == 0) {                   
         contact.setContactNumber(cur.getString(cur.getColumnIndex(Phone.NUMBER)).replaceAll("\\D", ""));
    } else {                      
         contact.setContactNumber(contact.getContactNumber().toString().concat(", ").concat(cur.getString(cur.getColumnIndex(Phone.NUMBER)).replaceAll("\\D", "")));//One can add possible contacts "(-/,"
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top