简体   繁体   中英

multichose in phonebook android

I have one question. In my application I open the phonebook and pull out the name and number of the person you have chosen. Here is the code that I do:

public void showContactsChooser(){
        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent, 1001);
    }

    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data){
        super.onActivityResult(reqCode, resultCode, data);

        switch(reqCode){
            case (1001):
                if (resultCode == Activity.RESULT_OK){
                    ContentResolver cr = getContentResolver();
                    Uri contactData = data.getData();
                    Cursor c = cr.query(contactData, null, null, null, null);
                    String phone = "";
                    if (c.moveToFirst()){
                        String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                                "DISPLAY_NAME = '" + name + "'", null, null);
                        if (cursor.moveToFirst()) {
                            String contactId =
                                    cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                            //
                            //  Get all phone numbers.
                            //
                            Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                            while (phones.moveToNext()) {
                                phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                                switch (type) {
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                        break;
                                }
                            }
                            phones.close();
                        Toast.makeText(getApplicationContext(), name + " " + phone, Toast.LENGTH_SHORT).show();
                        Log.d("PICK", "Contact " + name + " " + phone);
                    }
                }
            }
        }
    }

In this piece of code I open the phone book and when you click on a contact name and get a phone number, then go back to the old Activiti. But I can only choose one person. And I need to choose a few. My question:
Can I choose a few people in the phone book and then pass them to the application. And how to do it?

As far as I know there isn't any in-built way for selecting multiple contacts from contacts book!

What you can do is, fetch the contacts and display it in RecyclerView or ListView having custom adapter with checkbox. And then return those selected data back to your activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM