Android - 选择多个联系人

时间:2018-03-28 12:58:48

标签: java android

我知道这里有一些关于如何做到这一点的旧答案,但希望有更新的版本。

我想让用户从手机中选择多个联系人并将其添加到阵列中。 我正在使用ContactContract和PICK_CONTENT。

到目前为止,我有以下内容:

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

        switch (reqCode){
            case(PICK_CONTACT):
                if(resultCode == Activity.RESULT_OK){
                    Uri contactData = data.getData();
                    Cursor phone = getContentResolver().query(contactData,null,null,null,null);
                    PlayerDetails player = new PlayerDetails(); //TODO turn this into a loop to add each player the user picks and then display it
                    if(phone.moveToFirst()) {

                        player.name = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        player.number =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        playerListGame.addPlayer(player);

但是点击按钮启动此活动然后:

 public void showPlayerGameList(){
        playerTextView = findViewById(R.id.aPlayerBox);
        for(PlayerDetails i : playerListGame.myPlayers){
            playerTextView.append(i.name + "\n" + i.number); //TODO sort this out so it doesnt repeat the same names
        }
    }

然后我遍历我添加的数组,以显示我需要的信息。

我不知道如何让应用程序允许用户选择多个用户

由于

1 个答案:

答案 0 :(得分:0)

我知道现在已经很晚了,但我希望其他人甚至是你能以这种方式实施。

使用PICK_CONTACT启动联系人意图将无法选择多个联系人。即使您设法在某些设备上选择多个联系人,它也无法在大多数其他设备和Android系统版本上运行。正如我之前以同样的方式尝试过..

然后使用listview创建了multiple choce,并为列表视图的adapter提供了联系人列表。现在如何获取联系人列表..

ContactsList = new ArrayList<String>();
    ContactsNumbersList = new ArrayList<String>();

    HashSet<String> normalizedNumbersAlreadyFound = new HashSet<>();

    // Contacts Database queries

    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, null, null,  ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY +" ASC");


    while (cursor.moveToNext())
    {
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        phoneNumber = phoneNumber.replace("-","");
        phoneNumber = phoneNumber.replace(" ","");
        phoneNumber = phoneNumber.replace("(","");
        phoneNumber = phoneNumber.replace(")","");

        if (normalizedNumbersAlreadyFound.add(phoneNumber))
        {
            ContactsList.add(name);
            ContactsNumbersList.add(phoneNumber);
        }

    }
    cursor.close();