Android 联系人选择Widget

先看一下目录结构:
Android 联系人选择Widget

AndroidManifest.xml配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.iaiai.activity" android:versionCode="1"
	android:versionName="1.0">
	<uses-sdk android:minSdkVersion="8" />

	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name="ContactSelectorWidget"
			android:label="@string/contact_title">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	</application>
	<!-- 读取联系人 -->
	<uses-permission android:name="android.permission.READ_CONTACTS" />
	<!-- 写入联系人 -->
	<uses-permission android:name="android.permission.WRITE_CONTACTS" />
	<!-- 在SD卡中创建文件与删除文件权限 -->
	<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
	<!-- 往SD卡中写入数据权限 -->
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	<!-- 电话拨号 -->
	<uses-permission android:name="android.permission.CALL_PHONE" />
	<!-- 访问网络 -->
	<uses-permission android:name="android.permission.INTERNET" />
	<!-- 访问Wi-Fi网络 -->
	<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>

values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">iaiai - Contact</string>
    <string name="contact_title">iaiai - Contact</string>
</resources>

values-zh-rCN/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">丸子联系人</string>
    <string name="contact_title">丸子联系人</string>
</resources>

contact.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<ListView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/contactListView">
	</ListView>
</LinearLayout>

contact_list.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="wrap_content" android:id="@+id/contactItems"
	android:paddingBottom="5dip" android:paddingLeft="12dip"
	android:paddingTop="5dip" android:paddingRight="12dip">
	<ImageView android:id="@+id/contact_img"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_alignParentLeft="true"></ImageView>
	<TextView android:layout_height="wrap_content"
		android:layout_width="wrap_content" android:id="@+id/contact_name"
		android:textSize="20dip" android:paddingTop="12px"
		android:layout_marginLeft="70px">
	</TextView>
	<CheckBox android:id="@+id/contact_checkbox"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_alignParentRight="true" android:focusable="false"></CheckBox>
</RelativeLayout>

ContactSelectorWidget.java类:
package com.iaiai.activity;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import com.iaiai.activity.adapter.ContactCheckBoxAdapter;

public class ContactSelectorWidget extends Activity {

	public static final String CONTACT_NAME = "NAME"; // 保存的名字

	public static final String CONTACT_ID = "ID"; // ID

	public static final String CONTACT_FACE = "FACE"; // 头像

	private ContactCheckBoxAdapter contactCheckBoxAdapter; // 配置适配器

	private ListView list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact);

		list = (ListView) findViewById(R.id.contactListView);

		// 配置适配器
		contactCheckBoxAdapter = new ContactCheckBoxAdapter(this, getData()); // 布局里的控件id
		// 添加并且显示
		list.setAdapter(contactCheckBoxAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, 1, 0, android.R.string.ok);
		menu.add(Menu.NONE, 2, 0, android.R.string.cancel);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			Toast.makeText(
					this,
					"selector:"
							+ contactCheckBoxAdapter.onSelectorData().size(),
					Toast.LENGTH_LONG).show();
			break;
		case 2:
			Toast.makeText(this, android.R.string.cancel, Toast.LENGTH_LONG)
					.show();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * 获取联系人
	 * 
	 * @return
	 */
	private List<Map<String, Object>> getData() {
		// 组织数据源
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		ContentResolver contentResolver = getContentResolver();
		Cursor cursor = contentResolver.query(
				ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		while (cursor.moveToNext()) {
			String id = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts._ID)); // id
			String name = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); // 姓名

			Map<String, Object> map = new HashMap<String, Object>();
			map.put(CONTACT_ID, id);
			map.put(CONTACT_NAME, name);
			map.put(CONTACT_FACE, getContactBitmap(id));
			list.add(map);
		}
		cursor.close(); // 关闭
		return list;
	}

	/**
	 * 获得联系人头像
	 * 
	 * @param id
	 *            联系人id
	 * @return
	 */
	private Bitmap getContactBitmap(String id) {
		ContentResolver cr = getContentResolver();
		Uri uri = ContentUris.withAppendedId(
				ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
		InputStream input = ContactsContract.Contacts
				.openContactPhotoInputStream(cr, uri);
		if (input == null) {
			// Log.e("star","input is null");
			InputStream is = getResources().openRawResource(R.drawable.icon);
			BitmapDrawable bmpDraw = null;
			bmpDraw = new BitmapDrawable(is);
			Bitmap aa = null;
			aa = bmpDraw.getBitmap();
			Bitmap newb = Bitmap.createScaledBitmap(aa, 50, 50, false);
			return newb;
		} else {
			Bitmap contactPhoto = BitmapFactory.decodeStream(input);
			Bitmap newb = Bitmap
					.createScaledBitmap(contactPhoto, 50, 50, false);
			return newb;
		}
	}
}

ContactCheckBoxAdapter.java类:
package com.iaiai.activity.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.iaiai.activity.ContactSelectorWidget;
import com.iaiai.activity.R;

public class ContactCheckBoxAdapter extends BaseAdapter {

	private LayoutInflater mInflater;

	private List<Map<String, Object>> listData; // 数据

	private Map<Integer, Map<String, Object>> selectMap = new HashMap<Integer, Map<String, Object>>(); // 选择的数据

	private class ViewHolder {
		public ImageView img;
		public TextView title;
		public CheckBox checkBox;
	}

	public ContactCheckBoxAdapter(Context context,
			List<Map<String, Object>> listData) {
		this.mInflater = LayoutInflater.from(context);
		this.listData = listData;
	}

	@Override
	public int getCount() {
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * 返回选择的数据,Integer为选择的第几个
	 * 
	 * @return
	 */
	public Map<Integer, Map<String, Object>> onSelectorData() {
		return selectMap;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.contact_list, null);
			holder.img = (ImageView) convertView.findViewById(R.id.contact_img);
			holder.title = (TextView) convertView
					.findViewById(R.id.contact_name);
			holder.checkBox = (CheckBox) convertView
					.findViewById(R.id.contact_checkbox);
			holder.checkBox.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (selectMap.get(position) != null) {
						selectMap.remove(position);
					} else {
						selectMap.put(position, listData.get(position));
					}
				}
			});
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.img.setImageBitmap((Bitmap) listData.get(position).get(
				ContactSelectorWidget.CONTACT_FACE));
		holder.title.setText(listData.get(position)
				.get(ContactSelectorWidget.CONTACT_NAME).toString());

		if (selectMap.get(position) != null) {
			holder.checkBox.setChecked(true);
		} else {
			holder.checkBox.setChecked(false);
		}

		return convertView;
	}

}


运行结果:
Android 联系人选择Widget
Android 联系人选择Widget

你可能感兴趣的:(java,数据结构,android,xml,ITeye)