Android 联系人选择器清单

发布于 2024-09-18 20:06:20 字数 81 浏览 5 评论 0原文

我正在尝试创建一个“选择多个”联系人列表,其中我可以允许用户检查多个联系人。我正在寻找的内容实际上与向多个联系人撰写消息时出现的本机活动相同。谢谢!

I'm trying to create a "select-multiple" contact list where I can allow a user to check more than one contact. What I'm looking for is effectively the same thing as the native activity that appears when composing a message to multiple contacts. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2024-09-25 20:06:21

首先,您需要获取联系人列表,然后将其显示在 ListView 元素上。例如,我使用以下代码在 ListView 上显示所有用户联系人:

                 // Run query on all contacts id
                Uri uri = ContactsContract.Contacts.CONTENT_URI;
                String[] projection = new String[] { ContactsContract.Contacts._ID,
                                                ContactsContract.Contacts.DISPLAY_NAME};
                String selection = null;//ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '" + ("1") + "'";
                String[] selectionArgs = null;
                String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                        + " COLLATE LOCALIZED ASC";

                ContentResolver contectResolver = getContentResolver();

                Cursor cursor = contectResolver.query(uri, projection, selection, selectionArgs,
                        sortOrder);

                //Create buffer
                final ArrayList<ContactData> bufferContacts = new ArrayList<ContactData>();

                //Load contacts one by one
                if(cursor.moveToFirst()) {
                    while(!cursor.isAfterLast()) {



                        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));


                        String[] emailProj = new String[]{Email.DATA};
                        Cursor cursorEmail = contectResolver.query(Email.CONTENT_URI, emailProj,Email.CONTACT_ID + " = ? ", new String[] { id }, null);

                        String[] phoneProj = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
                        Cursor cursorPhone = contectResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, phoneProj,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);

                        String firstName = "";
                        String lastName = "";
                        String email = "";
                        String displayname = "";
                        String phoneNmb = "";

                        if(cursorPhone.moveToFirst()) {
                            ///displayname = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                            phoneNmb = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        cursorPhone.close();

                        if(cursorEmail.moveToFirst()) {
                            email = cursorEmail.getString(cursorEmail.getColumnIndex(Email.DATA));
                        }
                        cursorEmail.close();

                        displayname = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                        //Divide display name to first and last
                        String[] names = displayname.split("\\s+");

                        firstName = displayname;

                        if(names.length >= 1) {
                            firstName = names[0];
                        }

                        if(names.length >= 2) {
                            firstName = names[1];
                        }

                        final ContactData contactData = new ContactData(id, firstName, lastName, phoneNmb, email, allChecked);

                        bufferContacts.add(contactData);

                        //Set list view initialy
                        runOnUiThread(new Runnable() {
                            public void run() {

                                if(contactsAdapter == null) {
                                    ArrayList<ContactData> contacts = new ArrayList<ContactData>();
                                    contactsAdapter = new ContactAdapter(ContactPickerActivity.this, contacts);

                                    lvContacts.setAdapter(contactsAdapter);
                                }

                                if(bufferContacts.size() >= BUFFER_INTERVAL) {
                                    addBuffer(bufferContacts);
                                }
                            }

                        });

                        cursor.moveToNext();
                    }
                }

                cursor.close();

                runOnUiThread(new Runnable() {
                    public void run() {

                        addBuffer(bufferContacts);

                    }

                });

如果您不想从头开始做所有事情,那么请随意使用我现成的库来选择多个联系人:

https://github.com/kgadzinowski/Android-Multiple-Contacts-Picker-Library

At first you need to get a list of contacts and then display it on ListView element. For example I'm using following code to display all users contacts on a ListView:

                 // Run query on all contacts id
                Uri uri = ContactsContract.Contacts.CONTENT_URI;
                String[] projection = new String[] { ContactsContract.Contacts._ID,
                                                ContactsContract.Contacts.DISPLAY_NAME};
                String selection = null;//ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '" + ("1") + "'";
                String[] selectionArgs = null;
                String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                        + " COLLATE LOCALIZED ASC";

                ContentResolver contectResolver = getContentResolver();

                Cursor cursor = contectResolver.query(uri, projection, selection, selectionArgs,
                        sortOrder);

                //Create buffer
                final ArrayList<ContactData> bufferContacts = new ArrayList<ContactData>();

                //Load contacts one by one
                if(cursor.moveToFirst()) {
                    while(!cursor.isAfterLast()) {



                        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));


                        String[] emailProj = new String[]{Email.DATA};
                        Cursor cursorEmail = contectResolver.query(Email.CONTENT_URI, emailProj,Email.CONTACT_ID + " = ? ", new String[] { id }, null);

                        String[] phoneProj = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
                        Cursor cursorPhone = contectResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, phoneProj,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);

                        String firstName = "";
                        String lastName = "";
                        String email = "";
                        String displayname = "";
                        String phoneNmb = "";

                        if(cursorPhone.moveToFirst()) {
                            ///displayname = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                            phoneNmb = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        cursorPhone.close();

                        if(cursorEmail.moveToFirst()) {
                            email = cursorEmail.getString(cursorEmail.getColumnIndex(Email.DATA));
                        }
                        cursorEmail.close();

                        displayname = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                        //Divide display name to first and last
                        String[] names = displayname.split("\\s+");

                        firstName = displayname;

                        if(names.length >= 1) {
                            firstName = names[0];
                        }

                        if(names.length >= 2) {
                            firstName = names[1];
                        }

                        final ContactData contactData = new ContactData(id, firstName, lastName, phoneNmb, email, allChecked);

                        bufferContacts.add(contactData);

                        //Set list view initialy
                        runOnUiThread(new Runnable() {
                            public void run() {

                                if(contactsAdapter == null) {
                                    ArrayList<ContactData> contacts = new ArrayList<ContactData>();
                                    contactsAdapter = new ContactAdapter(ContactPickerActivity.this, contacts);

                                    lvContacts.setAdapter(contactsAdapter);
                                }

                                if(bufferContacts.size() >= BUFFER_INTERVAL) {
                                    addBuffer(bufferContacts);
                                }
                            }

                        });

                        cursor.moveToNext();
                    }
                }

                cursor.close();

                runOnUiThread(new Runnable() {
                    public void run() {

                        addBuffer(bufferContacts);

                    }

                });

If you don't want to bother yourself with making everything out of scratch then feel free to use my ready library for selecting multiple contacts:

https://github.com/kgadzinowski/Android-Multiple-Contacts-Picker-Library

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文