有很多网友问多选联系人实现方式,这里参考了apidemos的例子做了简单实现。
成都创新互联于2013年创立,先为城厢等服务建站,城厢等地企业,进行企业商务咨询服务。为城厢企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
整体思路是使用使用一个ArrayList存放选中的联系人信息,细节就不说了,贴一下代码
- public class CopyContactsListMultiple extends ListActivity implements OnClickListener{
- private final int UPDATE_LIST=1;
- ArrayList contactsList; //得到的所有联系人
- ArrayList getcontactsList; //选择得到联系人
- private Button okbtn;
- private Button cancelbtn;
- private ProgressDialog proDialog;
- Thread getcontacts;
- Handler updateListHandler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case UPDATE_LIST:
- if (proDialog != null) {
- proDialog.dismiss();
- }
- updateList();
- }
- }
- };
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.contactslist);
- contactsList=new ArrayList();
- getcontactsList=new ArrayList();
- final ListView listView = getListView();
- listView.setItemsCanFocus(false);
- listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
- okbtn=(Button)findViewById(R.id.contacts_done_button);
- cancelbtn=(Button)findViewById(R.id.contact_back_button);
- okbtn.setOnClickListener(this);
- cancelbtn.setOnClickListener(this);
- getcontacts=new Thread(new GetContacts());
- getcontacts.start();
- proDialog = ProgressDialog.show(CopyContactsListMultiple.this, “loading”,“loading”, true, true);
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- }
- void updateList(){
- if(contactsList!=null)
- setListAdapter(new ArrayAdapter(this,
- Android.R.layout.simple_list_item_multiple_choice, contactsList));
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- // TODO Auto-generated method stub
- if(!((CheckedTextView)v).isChecked()){
- CharSequence num=((CheckedTextView)v).getText();
- getcontactsList.add(num.toString());
- }
- if(((CheckedTextView)v).isChecked()){
- CharSequence num=((CheckedTextView)v).getText();
- if((num.toString()).indexOf(“[")>0){
- String phoneNum=num.toString().substring(0, (num.toString()).indexOf("\n"));
- getcontactsList.remove(phoneNum);
- Log.d("remove_num", ""+phoneNum);
- }else{
- getcontactsList.remove(num.toString());
- Log.d("remove_num", ""+num.toString());
- }
- }
- super.onListItemClick(l, v, position, id);
- }
- class GetContacts implements Runnable{
- @Override
- public void run() {
- // TODO Auto-generated method stub
- Uri uri = ContactsContract.Contacts.CONTENT_URI;
- String[] projection = new String[] {
- ContactsContract.Contacts._ID,
- ContactsContract.Contacts.DISPLAY_NAME,
- ContactsContract.Contacts.PHOTO_ID
- };
- String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + “ = ’1′”;
- String[] selectionArgs = null;
- String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + “ COLLATE LOCALIZED ASC”;
- Cursor cursor=managedQuery(uri, projection, selection, selectionArgs, sortOrder);
- Cursor phonecur = null;
- while (cursor.moveToNext()){
- // 取得联系人名字
- int nameFieldColumnIndex = cursor.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);
- String name = cursor.getString(nameFieldColumnIndex);
- // 取得联系人ID
- String contactId = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));
- phonecur = managedQuery(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID + “ = ” + contactId, null, null);
- // 取得电话号码(可能存在多个号码)
- while (phonecur.moveToNext()){
- String strPhoneNumber = phonecur.getString(phonecur.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));
- if(strPhoneNumber.length()>4)
- contactsList.add(“18610011001″+“\n测试”);
- //contactsList.add(strPhoneNumber+”\n”+name+”");
- }
- }
- if(phonecur!=null)
- phonecur.close();
- cursor.close();
- Message msg1=new Message();
- msg1.what=UPDATE_LIST;
- updateListHandler.sendMessage(msg1);
- }
- }
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- }
- @Override
- protected void onDestroy() {
- contactsList.clear();
- getcontactsList.clear();
- super.onDestroy();
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch (v.getId()) {
- case R.id.contacts_done_button:
- Intent i = new Intent();
- if(getcontactsList!=null>>getcontactsList.size()>0){
- Bundle b = new Bundle();
- b.putStringArrayList(“GET_CONTACT”, getcontactsList);
- i.putExtras(b);
- }
- setResult(RESULT_OK, i);
- CopyContactsListMultiple.this.finish();
- break;
- case R.id.contact_back_button:
- CopyContactsListMultiple.this.finish();
- break;
- default:
- break;
- }
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- // TODO Auto-generated method stub
- if(keyCode==KeyEvent.KEYCODE_BACK){
- Intent i = new Intent();
- Bundle b = new Bundle();
- b.putStringArrayList(“GET_CONTACT”, getcontactsList);
- i.putExtras(b); // }
- setResult(RESULT_OK, i);
- }
- return super.onKeyDown(keyCode, event);
- }
- }
xml:
- android:orientation=“vertical” android:layout_width=“fill_parent”
- android:layout_height=“fill_parent”>
- android:layout_height=“fill_parent”
- android:layout_width=“fill_parent”
- android:layout_marginLeft=“10dip”
- android:layout_marginRight=“10dip”
- android:layout_marginTop=“10dip”
- android:layout_weight=“1.0″>
- android:layout_height=“wrap_content”
- android:layout_weight=“0″ android:orientation=“horizontal”
- android:gravity=“center” android:layout_marginLeft=“10dip”
- android:layout_marginRight=“10dip” android:layout_marginBottom=“10dip”
- android:weightSum=“1″>
效果如图:
【编辑推荐】
新闻名称:Android实现多选联系人
转载来源:http://www.shufengxianlan.com/qtweb/news41/275441.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联