Vijaya
Vijaya

Reputation: 564

How to select multiple contacts at a time?

I retrieve phonebook contacts to my application by using following code:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  
                                            Contacts.CONTENT_URI);  
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);      
}

But I want to select multiple contacts and upload those to a DB. Is this possible and if so how can I do this?

Upvotes: 3

Views: 5941

Answers (2)

senola
senola

Reputation: 782

Well you could query the content provider directly inside your activity so the user can select multiple contact. This can be achieved using the contacts contract. For more information you can look at the api documentation or this tutorial blog post

Upvotes: 1

kannappan
kannappan

Reputation: 2250

try this code..

     people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    int position=0;
    Cursor q=db.query(mProfile,new String[]{"person_name"},"person_name"+"!='"+null+"'",null,null, null, null);
    people.moveToFirst();
        int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);    
        while(!people.isAfterLast()) {
            Cursor o=db.query(mProfile,new String[]{"person_name"},"person_name"+"='"+people.getString(nameFieldColumnIndex)+"'",null,null, null, null);
            if(!(o.getCount()>0))
            {  
            mConname.add(position, people.getString(nameFieldColumnIndex));
            try{
                String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
                String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                if ( hasPhone.equalsIgnoreCase("1"))
                    hasPhone = "true";
                else
                    hasPhone = "false" ;
                if (Boolean.parseBoolean(hasPhone)) 
                {
                    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                    while (phones.moveToNext()) 
                    {
                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        mConno.add(position,phoneNumber);

                    }
                    phones.close(); 
                }   
                if(hasPhone=="false")
                {   mConname.remove(position);
                }   
                else
                    position++;
            }       
            catch(Exception e)
            { 

            }
        }
            people.moveToNext();
        }

use the sqlite and store it

Upvotes: 0

Related Questions