新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android實(shí)現(xiàn)多選聯(lián)系人
有很多網(wǎng)友問多選聯(lián)系人實(shí)現(xiàn)方式,這里參考了apidemos的例子做了簡單實(shí)現(xiàn)。

成都創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為城廂等服務(wù)建站,城廂等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為城廂企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
整體思路是使用使用一個(gè)ArrayList存放選中的聯(lián)系人信息,細(xì)節(jié)就不說了,貼一下代碼
- public class CopyContactsListMultiple extends ListActivity implements OnClickListener{
- private final int UPDATE_LIST=1;
- ArrayList contactsList; //得到的所有聯(lián)系人
- ArrayList getcontactsList; //選擇得到聯(lián)系人
- 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, “l(fā)oading”,“l(fā)oading”, 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()){
- // 取得聯(lián)系人名字
- int nameFieldColumnIndex = cursor.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);
- String name = cursor.getString(nameFieldColumnIndex);
- // 取得聯(lián)系人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);
- // 取得電話號(hào)碼(可能存在多個(gè)號(hào)碼)
- while (phonecur.moveToNext()){
- String strPhoneNumber = phonecur.getString(phonecur.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));
- if(strPhoneNumber.length()>4)
- contactsList.add(“18610011001″+“\n測(cè)試”);
- //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編程方法大PK:NDK vs. RenderScript
- Android SQLite3命令詳解教程
- 如何開發(fā)基于Adobe AIR的Android應(yīng)用
標(biāo)題名稱:Android實(shí)現(xiàn)多選聯(lián)系人
本文路徑:http://www.dlmjj.cn/article/coghdpp.html


咨詢
建站咨詢
