如何选择使用复选框手机的多个联系人 [英] How to select multiple contacts from the phone using checkboxes

查看:131
本文介绍了如何选择使用复选框手机的多个联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择从手机可用的接触编程,我使用下面的code

I am trying to select contacts available from the phone programmatically and I am using the below code

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
        startActivityForResult(intent, 1);

然而,问题是怎样才能一次选择多个联系人在联系人页面使用复选框?

However the question is How can I select multiple contacts at a time by using a checkbox in the contacts page?

推荐答案

您将不得不以编程方式读取联系人,并在你的<$ C $显示他们在一个的ListView C>活动。使用复选框 S在该的ListView 项,并允许多个项目被选中。找一个简单的例子/教程一个的ListView ,并从那里开始。

You will have to read the Contacts programmatically and display them in a ListView in your Activity. Use CheckBoxs in the ListView items and allow multiple items to be selected. Find a simple example/tutorial for a ListView and start from there.

有几个原因,最好是创建一个自定义的ListView 而不是使用意图(Intent.ACTION_GET_CONTENT);

There are several reasons why it is better to create a custom ListView rather than using Intent(Intent.ACTION_GET_CONTENT);:

  1. 在可能没有办法选择的倍数按你们的要求。
  2. 即使你找到一个方式来选择倍数,这将是不同的 每个操作系统版本和设备,并可能无法在所有这些工作。
  3. 如果没有安装任何设备上的多个应用程序,可以 办理 ACTION_GET_CONTENT ,那么选择器将psented到$ P $ 用户,他将不得不选择其中的一个。用户的选择 可能不支持选择多个联系人。
  1. There may not be a way to select multiples as you have requested.
  2. Even if you find a way to select multiples, it will be different on every OS version and device, and might not work on all of them.
  3. If there are multiple applications installed on any device that can handle ACTION_GET_CONTENT, then a chooser will be presented to the user and he will have to select one of those. The user's selection may not support selecting multiple contacts.

下面是读你的系统接触的一个例子:


Here is an example that reads your system contacts:

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
while (cursor.moveToNext()) {
    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    if("1".equals(hasPhone) || Boolean.parseBoolean(hasPhone)) { 
        // You know it has a number so now query it like this
        Cursor phones = myActivity.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));
            int itype = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

            final boolean isMobile =
                itype == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE ||
                itype == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE;

            // Do something here with 'phoneNumber' such as saving into 
            // the List or Array that will be used in your 'ListView'.

        } 
        phones.close();
    }
}

这篇关于如何选择使用复选框手机的多个联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆