从android中的电话簿中选择多个联系人
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21200437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select multiple contacts from phone book in android
提问by Kartheek s
I need to select contact numbers/emails from phone book in android.
我需要从 android 的电话簿中选择联系号码/电子邮件。
I have seen selecting one contact and getting the result back in onActivityResultfrom this link.
我已经看到选择一个联系人并onActivityResult从此链接返回结果。
But I need multiple contacts to be selected from the phone book. How to achieve this?
但是我需要从电话簿中选择多个联系人。如何实现这一目标?
I don't want to make my custom list, is there a way to use androids built in functionality?
我不想制作我的自定义列表,有没有办法使用 androids 的内置功能?
回答by anupam sharma
I am sharing code for select multiple contact from Phone Book. you have change little bit and you can achieve your goal. thanks
我正在共享用于从电话簿中选择多个联系人的代码。你有一点改变,你可以实现你的目标。谢谢
getAllContacts(this.getContentResolver());
ListView lv = (ListView) findViewById(R.id.lv);
ma = new MyAdapter();
lv.setAdapter(ma);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
// adding
select = (Button) findViewById(R.id.button1);
select.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder checkedcontacts = new StringBuilder();
for (int i = 0; i < name1.size(); i++)
{
if (ma.mCheckStates.get(i) == true) {
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
} else {
}
}
Toast.makeText(Display.this, checkedcontacts, 1000).show();
}
});
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
ma.toggle(arg2);
}
public void getAllContacts(ContentResolver cr) {
Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
name1.add(name);
phno1.add(phoneNumber);
}
phones.close();
}
class MyAdapter extends BaseAdapter implements
CompoundButton.OnCheckedChangeListener {
private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView tv1, tv;
CheckBox cb;
MyAdapter() {
mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater) Display.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return name1.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = mInflater.inflate(R.layout.row, null);
tv = (TextView) vi.findViewById(R.id.textView1);
tv1 = (TextView) vi.findViewById(R.id.textView2);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText("Name :" + name1.get(position));
tv1.setText("Phone No :" + phno1.get(position));
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
回答by sandrstar
Looks like there's no such 'official' way. Your question is basically the same as this one How to select multiple contacts with Android SDKwhich has reference to custom implementation.
看起来没有这样的“官方”方式。您的问题与如何使用Android SDK 选择多个联系人的问题基本相同,其中引用了自定义实现。
If You check AOSP Contacts application, there's no such possibility neither in documentation. The only thing I've observed from the source of AOSP Contacts is the following mention (in com.android.contacts.activities.PeopleActivity):
如果您检查 AOSP 联系人应用程序,则文档中都没有这种可能性。我从 AOSP 联系人的来源中观察到的唯一一件事是以下提及(在com.android.contacts.activities.PeopleActivity 中):
// TODO fix or remove multipicker code
// else if (resultCode == RESULT_CANCELED && mMode == MODE_PICK_MULTIPLE_PHONES) {
From MMS app source codeYou can observe the following (in ComposeMessageActivity):
从MMS 应用程序源代码您可以观察到以下内容(在ComposeMessageActivity 中):
private void launchMultiplePhonePicker() {
Intent intent = new Intent(Intents.ACTION_GET_MULTIPLE_PHONES);
intent.addCategory("android.intent.category.DEFAULT");
intent.setType(Phone.CONTENT_TYPE);
// We have to wait for the constructing complete.
ContactList contacts = mRecipientsEditor.constructContactsFromInput(true);
int urisCount = 0;
Uri[] uris = new Uri[contacts.size()];
urisCount = 0;
for (Contact contact : contacts) {
if (Contact.CONTACT_METHOD_TYPE_PHONE == contact.getContactMethodType()) {
uris[urisCount++] = contact.getPhoneUri();
}
}
if (urisCount > 0) {
intent.putExtra(Intents.EXTRA_PHONE_URIS, uris);
}
startActivityForResult(intent, REQUEST_CODE_PICK);
}
Field Intents.ACTION_GET_MULTIPLE_PHONESis available in ContactsContract.javabut I wasn't able to find any usage of it across AOSP. So, I think it's some dead code or that action get handled in some closed code.
I've tried the same way to launch contacts selection from my test application and got only exception about no application to handle the action.
字段Intents.ACTION_GET_MULTIPLE_PHONES在ContactsContract.java中可用,但我无法在 AOSP 中找到它的任何用法。所以,我认为这是一些死代码,或者在一些封闭的代码中处理了该操作。我尝试了相同的方法从我的测试应用程序启动联系人选择,但只有没有应用程序来处理该操作的异常。
Of course, vendors Contacts applications provide such abilities (e.g. then selecting messages recipient), but it's better not to rely on them.
当然,供应商联系人应用程序提供了这样的能力(例如选择消息接收者),但最好不要依赖它们。

