简体   繁体   English

Android多个联系人选择器(可选择选择哪个电话号码)

[英]Android Multiple Contacts Chooser (with option of choosing which phone number)

I need to be able to select multiple contacts in Android. 我需要能够在Android中选择多个联系人。 The flow is like this : 流程是这样的:

  1. User clicks on a button which opens the Contacts application. 用户单击打开“联系人”应用程序的按钮。
  2. However, instead of being able to select a single contact, I need to be able to select multiple contacts (in the same launch of the intent). 但是,我不需要选择单个联系人,而是需要能够选择多个联系人(在意图的同一启动中)。
  3. If a contact has multiple phone numbers, I need the user to be able to choose which phone number he wants to select. 如果联系人有多个电话号码,我需要用户能够选择他想要选择的电话号码。

This feature is already present in my Samsung Android Phone (Running 2.3 Gingerbread) when I click on "Contacts" in the Messaging app. 当我点击消息应用程序中的“联系人”时,我的三星Android手机(运行2.3姜饼)中已经存在此功能。 See screenshot below : 见下面的截图:

在此输入图像描述

在此输入图像描述

在此输入图像描述

There is not built in way of doing this, so you need to do most of the work by yourself. 没有内置的方法,所以你需要自己完成大部分工作。 Luckily, it's not that hard. 幸运的是,这并不难。

Display 显示

To Display your contacts you can use either a listview with the multi-select choice mode, or you can create a custom adapter and bind it to a regular listview. 要显示联系人,您可以使用带有多选选择模式的列表视图,也可以创建自定义适配器并将其绑定到常规列表视图。 I don't think the listview with multi-select will let you put anything other than text for each row, but you'd have to dig deeper to find out. 我不认为带有多选的列表视图会让你为每一行添加除文本之外的任何内容,但你必须深入挖掘才能找到答案。

I've used the custom adapter method for something very similar (except the multiple phone numbers part). 我已经将自定义适配器方法用于非常相似的事情(除了多个电话号码部分)。 It's pretty easy to do and I find custom adapters are really useful in the long run. 这很容易做到,我发现自定义适配器从长远来看非常有用。

Custom Adapter Listview Tutorial 自定义适配器Listview教程

With a custom adapter setup, you can create data objects with all the information for a person including their Name and Phone Number(s). 使用自定义适配器设置,您可以创建包含人员所有信息的数据对象,包括其姓名和电话号码。 In the getView of your Custom Adapter, you can decide what/how and where to display each piece of information. 在自定义适配器的getView中,您可以决定显示每条信息的内容/方式和位置。

Gathering Information 收集信息

You'll need to use the ContactContract API to get information for your contacts. 您需要使用ContactContract API来获取联系人的信息。

Reading Contact Info 阅读联系信息

Reading ALL phone numbers for a Contact 读取联系人的所有电话号码

You will have to write this all yourself. 你必须自己写这个。 You can use the ContactsContract provider to query for all contacts with phone numbers, and then for the selected contact you can query for all phone numbers for that contact. 您可以使用ContactsContract提供程序查询具有电话号码的所有联系人,然后对于所选联系人,您可以查询该联系人的所有电话号码。 You can display the results in activities or dialogs as you see fit. 您可以根据需要在活动或对话框中显示结果。

Unfortunately this code isn't supported for all versions of android 不幸的是,所有版本的android都不支持此代码

I know it's kinda late but wanted to share this! 我知道有点晚了但想分享一下! I found some incomplete code in the net and after cracking my head with it I finally found the answer! 我在网上发现了一些不完整的代码,在用它解开后我终于找到了答案! Basically you launch the picker and let it return the data in the extras =] 基本上你启动选择器并让它返回extras =]中的数据

There was no full answer in the net so hope it helps to some soul out there! 网上没有完整的答案,所以希望它对那里的灵魂有所帮助!

Enjoy: 请享用:

    public void pickContact(View v){
    try {
        Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU");
        phonebookIntent.putExtra("additional", "phone-multi");
        startActivityForResult(phonebookIntent, PICK_CONTACT); 
        // PICK_CONTACT IS JUST AN INT HOLDING SOME NUMBER OF YOUR CHOICE

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public String getData(String contact, int which)
{
    return contact.split(";")[which];
}

public void onActivityResult(int reqCode, int resultCode, Intent data) {
    final int URI = 0;
    final int NUMBER = 1;

    if (RESULT_OK != resultCode) return;
    Bundle contactUri = data.getExtras();
    if (null == contactUri) return;

    ArrayList<String> contacts = (ArrayList<String>)contactUri.get("result");
    Toast.makeText(getApplicationContext(), getData(contacts.get(0),NUMBER), Toast.LENGTH_SHORT).show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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