自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android實(shí)現(xiàn)多選聯(lián)系人

移動開發(fā) Android
有很多網(wǎng)友問多選聯(lián)系人實(shí)現(xiàn)方式,這里參考了apidemos的例子做了簡單實(shí)現(xiàn)。整體思路是使用使用一個ArrayList存放選中的聯(lián)系人信息,細(xì)節(jié)就不說了,貼一下代碼

有很多網(wǎng)友問多選聯(lián)系人實(shí)現(xiàn)方式,這里參考了apidemos的例子做了簡單實(shí)現(xiàn)。

整體思路是使用使用一個ArrayList存放選中的聯(lián)系人信息,細(xì)節(jié)就不說了,貼一下代碼

  1. public class CopyContactsListMultiple extends ListActivity implements OnClickListener{  
  2.    
  3. private final int UPDATE_LIST=1;  
  4. ArrayList contactsList; //得到的所有聯(lián)系人  
  5. ArrayList getcontactsList; //選擇得到聯(lián)系人  
  6. private Button okbtn;  
  7. private Button cancelbtn;  
  8. private ProgressDialog proDialog;  
  9.    
  10. Thread getcontacts;  
  11. Handler updateListHandler = new Handler() {  
  12. public void handleMessage(Message msg) {  
  13. switch (msg.what) {  
  14.    
  15. case UPDATE_LIST:  
  16. if (proDialog != null) {  
  17. proDialog.dismiss();  
  18. }  
  19. updateList();  
  20. }  
  21. }  
  22. };  
  23. public void onCreate(Bundle savedInstanceState) {  
  24. super.onCreate(savedInstanceState);  
  25. setContentView(R.layout.contactslist);  
  26. contactsList=new ArrayList();  
  27. getcontactsList=new ArrayList();  
  28.    
  29. final ListView listView = getListView();  
  30. listView.setItemsCanFocus(false);  
  31. listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);  
  32. okbtn=(Button)findViewById(R.id.contacts_done_button);  
  33. cancelbtn=(Button)findViewById(R.id.contact_back_button);  
  34. okbtn.setOnClickListener(this);  
  35. cancelbtn.setOnClickListener(this);  
  36.    
  37. getcontacts=new Thread(new GetContacts());  
  38. getcontacts.start();  
  39. proDialog = ProgressDialog.show(CopyContactsListMultiple.this, “loading”,“loading”, truetrue);  
  40.    
  41. }  
  42.    
  43. @Override 
  44. protected void onResume() {  
  45. // TODO Auto-generated method stub  
  46. super.onResume();  
  47.    
  48. }  
  49.    
  50. void updateList(){  
  51. if(contactsList!=null)  
  52. setListAdapter(new ArrayAdapter(this,  
  53. android.R.layout.simple_list_item_multiple_choice, contactsList));  
  54.    
  55. }  
  56.    
  57. @Override 
  58. protected void onListItemClick(ListView l, View v, int position, long id) {  
  59. // TODO Auto-generated method stub  
  60. if(!((CheckedTextView)v).isChecked()){  
  61.    
  62. CharSequence num=((CheckedTextView)v).getText();  
  63. getcontactsList.add(num.toString());  
  64. }  
  65. if(((CheckedTextView)v).isChecked()){  
  66. CharSequence num=((CheckedTextView)v).getText();  
  67. if((num.toString()).indexOf(“[")>0){  
  68. String phoneNum=num.toString().substring(0, (num.toString()).indexOf("\n"));  
  69. getcontactsList.remove(phoneNum);  
  70. Log.d("remove_num"""+phoneNum);  
  71. }else{  
  72. getcontactsList.remove(num.toString());  
  73. Log.d("remove_num"""+num.toString());  
  74. }  
  75. }  
  76. super.onListItemClick(l, v, position, id);  
  77. }  
  78. class GetContacts implements Runnable{  
  79. @Override 
  80. public void run() {  
  81. // TODO Auto-generated method stub  
  82. Uri uri = ContactsContract.Contacts.CONTENT_URI;  
  83. String[] projection = new String[] {  
  84. ContactsContract.Contacts._ID,  
  85. ContactsContract.Contacts.DISPLAY_NAME,  
  86. ContactsContract.Contacts.PHOTO_ID  
  87. };  
  88. String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + “ = ’1′”;  
  89. String[] selectionArgs = null;  
  90. String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + “ COLLATE LOCALIZED ASC”;  
  91. Cursor cursor=managedQuery(uri, projection, selection, selectionArgs, sortOrder);  
  92. Cursor phonecur = null;  
  93.    
  94. while (cursor.moveToNext()){  
  95.    
  96. // 取得聯(lián)系人名字  
  97. int nameFieldColumnIndex = cursor.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);  
  98. String name = cursor.getString(nameFieldColumnIndex);  
  99. // 取得聯(lián)系人ID 

 

  1. String contactId = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));  
  2. phonecur = managedQuery(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID + “ = ” + contactId, null, null);  
  3. // 取得電話號碼(可能存在多個號碼)  
  4. while (phonecur.moveToNext()){  
  5. String strPhoneNumber = phonecur.getString(phonecur.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));  
  6. if(strPhoneNumber.length()>4)  
  7. contactsList.add(“18610011001″+“\n測試”);  
  8. //contactsList.add(strPhoneNumber+”\n”+name+”");  
  9.    
  10. }  
  11. }  
  12. if(phonecur!=null)  
  13. phonecur.close();  
  14. cursor.close();  
  15.    
  16. Message msg1=new Message();  
  17. msg1.what=UPDATE_LIST;  
  18. updateListHandler.sendMessage(msg1);  
  19. }  
  20. }  
  21. @Override  
  22. protected void onPause() {  
  23. // TODO Auto-generated method stub  
  24. super.onPause();  
  25.    
  26. }  
  27.    
  28. @Override  
  29. protected void onDestroy() {  
  30. contactsList.clear();  
  31. getcontactsList.clear();  
  32. super.onDestroy();  
  33. }  
  34.    
  35. @Override  
  36. public void onClick(View v) {  
  37. // TODO Auto-generated method stub  
  38. switch (v.getId()) {  
  39. case R.id.contacts_done_button:  
  40. Intent i = new Intent();  
  41. if(getcontactsList!=null>>getcontactsList.size()>0){  
  42. Bundle b = new Bundle();  
  43. b.putStringArrayList(“GET_CONTACT”, getcontactsList);  
  44. i.putExtras(b);  
  45. }  
  46. setResult(RESULT_OK, i);  
  47. CopyContactsListMultiple.this.finish();  
  48. break;  
  49. case R.id.contact_back_button:  
  50. CopyContactsListMultiple.this.finish();  
  51. break;  
  52. default:  
  53. break;  
  54. }  
  55. }  
  56. @Override  
  57. public boolean onKeyDown(int keyCode, KeyEvent event) {  
  58. // TODO Auto-generated method stub  
  59. if(keyCode==KeyEvent.KEYCODE_BACK){  
  60. Intent i = new Intent();  
  61. Bundle b = new Bundle();  
  62. b.putStringArrayList(“GET_CONTACT”, getcontactsList);  
  63. i.putExtras(b); // }  
  64. setResult(RESULT_OK, i);  
  65. }  
  66. return super.onKeyDown(keyCode, event);  
  67. }  

xml:

  1. <?xml version=“1.0″ encoding=“utf-8″?> 
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.         android:orientation=“vertical” android:layout_width=“fill_parent”  
  4.         android:layout_height=“fill_parent”> 
  5.    
  6.    
  7.         <ListView android:id=“@+id/android:list”   
  8.                 android:layout_height=“fill_parent”   
  9.                 android:layout_width=“fill_parent”  
  10.                  android:layout_marginLeft=“10dip”  
  11.                 android:layout_marginRight=“10dip”   
  12.                 android:layout_marginTop=“10dip”  
  13.                 android:layout_weight=“1.0″> 
  14.         </ListView> 
  15.    
  16.         <LinearLayout android:layout_width=“fill_parent”  
  17.                 android:layout_height=“wrap_content”  
  18.                 android:layout_weight=“0″ android:orientation=“horizontal”  
  19.                 android:gravity=“center” android:layout_marginLeft=“10dip”  
  20.                 android:layout_marginRight=“10dip” android:layout_marginBottom=“10dip”  
  21.                 android:weightSum=“1″> 
  22.    
  23.                 <Button android:id=“@+id/contacts_done_button”  
  24.                         android:textSize=“17dip”  
  25.                         android:layout_marginRight=“10dip” android:layout_width=“0dip”  
  26.                         android:layout_height=“wrap_content” android:layout_weight=“0.35″  
  27.                         android:text=“sure” /> 
  28.    
  29.                 <Button android:id=“@+id/contact_back_button”  
  30.                         android:layout_marginLeft=“10dip” android:textSize=“17dip”  
  31.                         android:layout_width=“0dip” android:layout_height=“wrap_content”  
  32.                         android:layout_weight=“0.35″ android:text=“back” /> 
  33.         </LinearLayout> 
  34.    
  35. </LinearLayout> 

效果如圖:

 

device-2012-02-06-091606.png

【編輯推薦】

  1. Android編程方法大PK:NDK vs. RenderScript 
  2. Android SQLite3命令詳解教程 
  3. 如何開發(fā)基于Adobe AIR的Android應(yīng)用 
責(zé)任編輯:冰凝兒 來源: DEVDIV博客
相關(guān)推薦

2014-12-30 11:51:35

ListViewItem View

2011-05-26 14:42:34

Android 手機(jī)

2010-01-27 14:08:56

Android查詢聯(lián)系

2015-01-21 15:50:55

Android源碼全國城市列表

2015-11-11 10:17:15

ios9聯(lián)系人框架干貨

2020-02-02 14:45:55

聯(lián)系人開源工具

2011-08-12 10:16:10

iPhone通訊錄聯(lián)系人

2013-09-17 09:51:49

谷歌Bump移動應(yīng)用

2012-03-26 21:38:36

智能

2012-02-24 09:25:58

2019-11-07 09:20:36

Windows 10聯(lián)系人Outlook

2011-09-21 14:33:17

點(diǎn)心

2013-05-07 09:26:26

Office 365微軟

2012-02-02 17:16:11

Windows PhoC#聯(lián)系人資料

2015-09-24 11:37:43

2011-10-14 09:42:06

點(diǎn)心通訊錄

2020-04-29 09:55:13

蘋果谷歌API

2010-11-23 11:21:25

Microsoft L

2014-12-10 10:45:56

Android應(yīng)用權(quán)限

2022-01-04 15:34:31

鴻蒙HarmonyOS應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號