Android 实现 多选联系人

package com.example.tetmulticontacts;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity implements OnClickListener {

	private final int UPDATE_LIST = 1;
	ArrayList<String> contactsList; // 得到的所有联系人
	ArrayList<String> 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.activity_main);
		contactsList = new ArrayList<String>();
		getcontactsList = new ArrayList<String>();

		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(MainActivity.this, "loading",
				"loading", true, true);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	}

	void updateList() {
		if (contactsList != null)
			setListAdapter(new ArrayAdapter<String>(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()) {

			String num = ((CheckedTextView) v).getText().toString();
			num = num.substring(0, num.indexOf("\n"));
			getcontactsList.add(num.toString());
		}
		Log.e("onListItemClick", "==========");
		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.e("remove_num", "" + phoneNum);
			} else {
				getcontactsList.remove(num.toString());
				Log.e("remove_num", "" + num.toString());
			}
		}
		super.onListItemClick(l, v, position, id);
	}

	class GetContacts implements Runnable {
		@SuppressWarnings("deprecation")
		@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);
			}
			for (int j = 0; j < getcontactsList.size(); j++) {
				Log.e("onClick", getcontactsList.get(j));
			}
			setResult(RESULT_OK, i);
			MainActivity.this.finish();
			break;
		case R.id.contact_back_button:
			MainActivity.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 version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:layout_marginTop="10dip"
        android:layout_weight="1.0" >
    </ListView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:layout_weight="0"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="1" >

        <Button
            android:id="@+id/contacts_done_button"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dip"
            android:layout_weight="0.35"
            android:text="sure"
            android:textSize="17dip" />

        <Button
            android:id="@+id/contact_back_button"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_weight="0.35"
            android:text="back"
            android:textSize="17dip" />
    </LinearLayout>

</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值