How to select multiple contacts at a time?

╄→гoц情女王★ 提交于 2020-01-11 09:33:21

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/5675670/how-to-select-multiple-contacts-at-a-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!