首先给出实现完的界面图
上面是给出来的实现结果图。
从本地联系人中导出联系人,需要使用ContentProvider来获取通讯录中的联系人。
从通讯录中获取联系人方法如下:
- /** 得到手机通讯录联系人信息 **/
- private void getPhoneContacts() {
- ContentResolver resolver = ContactsActivity.this.getContentResolver();
- // 获取手机联系人
- Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI,
- null, null, null, null);
- if (cursor != null) {
- while (cursor.moveToNext()) {
- // 查看联系人有多少个号码,如果没有号码,返回0
- int idColumn = cursor
- .getColumnIndex(ContactsContract.Contacts._ID);
- int phoneCount = cursor
- .getInt(cursor
- .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
- int displayNameColumn = cursor
- .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
- // 得到联系人名称
- String contactName = cursor.getString(displayNameColumn);
- // 获得联系人的ID
- String contactId = cursor.getString(idColumn);
- List<String> list = new ArrayList<String>();
- if (phoneCount > 0) {
- // 获得联系人的电话号码列表
- Cursor phoneCursor = getContentResolver().query(
- ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
- null,
- ContactsContract.CommonDataKinds.Phone.CONTACT_ID
- + "=" + contactId, null, null);
- if (phoneCursor.moveToFirst()) {
- do {
- // 遍历所有的联系人下面所有的电话号码
- String phoneNumber = phoneCursor
- .getString(phoneCursor
- .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
- // 去掉手机号中的空格和“-”
- phoneNumber = phoneNumber.replace(" ", "")
- .replace("-", "").replace("+86", "");
- if (DataTools.isMobileNO(phoneNumber)) {
- if (!list.contains(phoneNumber)) {
- list.add(phoneNumber);
- }
- }
- } while (phoneCursor.moveToNext());
- }
- phoneCursor.close();
- }
- // //得到手机号码
- // String phoneNumber = cursor.getString(PHONES_NUMBER_INDEX);
- // 当手机号码为空的或者为空字段 跳过当前循环
- if (list.size() > 0) {
- if (!maps.containsKey(contactId)) {// 所有联系人放到maps中,
- ContactBean bean = new ContactBean();
- bean.setContactName(contactName);
- bean.setContactNum(list);
- bean.setCheck(false);
- maps.put(contactId, bean);
- }
- }
- }
- cursor.close();
- }
- if (maps.size() > 0) {// 有数据
- handler.sendEmptyMessage(1);
- } else {
- handler.sendEmptyMessage(0);
- }
- }
- public class ContactBean implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private String contactName;
- private List<String> contactNum;
- private boolean isCheck;
- private int positon = -1;
- private String addr;
- public String getContactName() {
- return contactName;
- }
- public void setContactName(String contactName) {
- this.contactName = contactName;
- }
- public List<String> getContactNum() {
- return contactNum;
- }
- public void setContactNum(List<String> contactNum) {
- this.contactNum = contactNum;
- }
- public boolean isCheck() {
- return isCheck;
- }
- public void setCheck(boolean isCheck) {
- this.isCheck = isCheck;
- }
- public int getPositon() {
- return positon;
- }
- public void setPositon(int positon) {
- this.positon = positon;
- }
- public String getAddr() {
- return addr;
- }
- public void setAddr(String addr) {
- this.addr = addr;
- }
- }
// 联系人一对一
private HashMap<String, ContactBean> maps = new HashMap<String, ContactBean>();
// 联系人姓名对号码一对多
private HashMap<String, ContactBean> nameMaps = new HashMap<String, ContactBean>();
// 联系人号码对姓名一对多
private HashMap<String, ContactBean> phoneMaps = new HashMap<String, ContactBean>();
private HashSet<String> set = new HashSet<String>();
处理数据分类的方法代码如下:
- public void dataClassify {
- for (Map.Entry<String, ContactBean> entry : maps.entrySet()) {// 遍历全部数据
- if (entry.getValue().getContactNum().size() > 1) {
- nameMaps.put(entry.getKey(), entry.getValue());// 将姓名号码一对多的放入nameMaps中
- } else {
- for (Map.Entry<String, ContactBean> entry1 : maps.entrySet()) {
- if (!entry.getValue().getContactName()
- .equals(entry1.getValue().getContactName())
- && entry.getValue()
- .getContactNum()
- .get(0)
- .equals(entry1.getValue().getContactNum()
- .get(0))) {
- // 将号码与姓名一对多的放入phoneMaps中
- phoneMaps.put(entry.getKey(), entry.getValue());
- phoneMaps.put(entry1.getKey(), entry1.getValue());
- }
- }
- }
- }
- for (Map.Entry<String, ContactBean> entry : nameMaps.entrySet()) {// 踢掉姓名号码一对多对象
- maps.remove(entry.getKey());
- nameList.add(entry.getValue());
- }
- for (Map.Entry<String, ContactBean> entry : phoneMaps.entrySet()) {// 踢掉号码姓名一对多对象
- maps.remove(entry.getKey());
- set.add(entry.getValue().getContactNum().get(0));
- }
- for (String str : set) {
- List<String> list = new ArrayList<String>();
- for (Map.Entry<String, ContactBean> entry : phoneMaps.entrySet()) {
- if (str.equals(entry.getValue().getContactNum().get(0))) {
- list.add(entry.getValue().getContactName());
- }
- }
- ContactBean contactBean = new ContactBean();
- contactBean.setContactName(str);
- contactBean.setContactNum(list);
- phoneList.add(contactBean);
- }
- for (Map.Entry<String, ContactBean> entry : maps.entrySet()) {// 放到数据集中进行适配
- dataList.add(entry.getValue());
- }
- // List<ContactBean> duList = new ArrayList<ContactBean>();
- for (int i = 0; i < dataList.size() - 1; i++) {// 去掉同名同电话数据
- for (int j = dataList.size() - 1; j > i; j--) {
- if (dataList.get(j).getContactName()
- .equals(dataList.get(i).getContactName())
- && dataList.get(j).getContactNum().get(0)
- .equals(dataList.get(i).getContactNum().get(0))) {
- dataList.remove(j);
- }
- }
- }
- adapter.notifyDataSetChanged();
- }
一对一联系人的列表适配类ContactListAdapter
- public class ContactListAdapter extends BaseAdapter {
- private Context mContext;
- private List<ContactBean> dataList;
- private TextView numTextView;
- public ContactListAdapter(Context context, List<ContactBean> list,TextView numTextView) {
- this.mContext = context;
- this.dataList = list;
- this.numTextView = numTextView;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return dataList != null ? dataList.size() : 0;
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- final int pos = position;
- final ViewHolder viewHolder;
- if (convertView == null) {
- viewHolder = new ViewHolder();
- convertView = LayoutInflater.from(mContext).inflate(
- R.layout.item_contact_import_listview, null);
- viewHolder.nameTextView = (TextView) convertView
- .findViewById(R.id.contact_import_name_txt);
- viewHolder.numTextView = (TextView) convertView
- .findViewById(R.id.contact_import_num_txt);
- viewHolder.checkBox = (CheckBox) convertView
- .findViewById(R.id.contact_import_checkbox);
- convertView.setTag(viewHolder);
- } else {
- viewHolder = (ViewHolder) convertView.getTag();
- }
- viewHolder.nameTextView
- .setText(dataList.get(position).getContactName());
- viewHolder.numTextView.setText(dataList.get(position).getContactNum()
- .get(0));
- viewHolder.checkBox.setOnCheckedChangeListener(null);
- if (dataList.get(position).isCheck()) {
- viewHolder.checkBox.setChecked(true);
- viewHolder.checkBox
- .setButtonDrawable(R.drawable.xuankuangxuanzhong);
- } else {
- viewHolder.checkBox.setChecked(false);
- viewHolder.checkBox.setButtonDrawable(R.drawable.xuankuangfang);
- }
- viewHolder.checkBox
- .setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- if (isChecked) {// 选中
- viewHolder.checkBox.setChecked(true);
- viewHolder.checkBox
- .setButtonDrawable(R.drawable.xuankuangxuanzhong);
- dataList.get(pos).setCheck(true);
- int i = getSelectItem().size();
- numTextView.setText("已选择"+i+"位联系人");
- } else {
- viewHolder.checkBox.setChecked(false);
- dataList.get(pos).setCheck(false);
- viewHolder.checkBox
- .setButtonDrawable(R.drawable.xuankuangfang);
- int i = getSelectItem().size();
- numTextView.setText("已选择"+i+"位联系人");
- }
- }
- });
- return convertView;
- }
- public List<ContactBean> getSelectItem(){
- List<ContactBean> list = new ArrayList<ContactBean>();
- for (int i = 0; i < dataList.size(); i++) {
- if(dataList.get(i).isCheck()){
- list.add(dataList.get(i));
- }
- }
- return list;
- }
- @Override
- public void notifyDataSetChanged() {
- // TODO Auto-generated method stub
- super.notifyDataSetChanged();
- numTextView.setText("已选择"+getSelectItem().size()+"位联系人");
- }
- public void selectAll(){
- for (int i = 0; i < dataList.size(); i++) {
- dataList.get(i).setCheck(true);
- }
- notifyDataSetChanged();
- }
- public void cancelAll(){
- for (int i = 0; i < dataList.size(); i++) {
- dataList.get(i).setCheck(false);
- }
- notifyDataSetChanged();
- }
- public static class ViewHolder {
- private TextView nameTextView;
- private TextView numTextView;
- private CheckBox checkBox;
- }
- }
一对多,多对一的列表适配类ContactSameNameListAdapter
- public class ContactSameNameListAdapter extends BaseAdapter {
- private Context mContext;
- private List<ContactBean> dataList;
- private TextView numTextView;
- private int type;//1为name2为phone
- private List<HashSet<String>> selectSet = new ArrayList<HashSet<String>>();
- public ContactSameNameListAdapter(Context context, List<ContactBean> list,
- TextView numTextView,int type) {
- this.mContext = context;
- this.dataList = list;
- this.numTextView = numTextView;
- this.type = type;
- for (int i = 0;dataList!=null&&i < dataList.size(); i++) {
- HashSet<String> set = new HashSet<String>();
- selectSet.add(set);
- }
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return dataList != null ? dataList.size() : 0;
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- final ViewHolder viewHolder;
- if (convertView == null) {
- viewHolder = new ViewHolder();
- convertView = LayoutInflater.from(mContext).inflate(
- R.layout.item_contact_same_name_listview, null);
- viewHolder.nameTextView = (TextView) convertView
- .findViewById(R.id.contact_import_same_txt);
- viewHolder.listview = (ListView) convertView
- .findViewById(R.id.contact_import_same_listview);
- convertView.setTag(viewHolder);
- } else {
- viewHolder = (ViewHolder) convertView.getTag();
- }
- ContactsSameItemListAdapter adapter = new ContactsSameItemListAdapter(
- mContext, dataList.get(position),dataList,position,numTextView,selectSet.get(position));
- viewHolder.listview.setAdapter(adapter);
- viewHolder.listview.setDividerHeight(0);
- viewHolder.nameTextView
- .setText(dataList.get(position).getContactName());
- setGridViewHeightBasedOnChildren(viewHolder.listview);
- return convertView;
- }
- public List<ContactBean> getSelectItem(){
- List<ContactBean> alist = new ArrayList<ContactBean>();
- for (int i = 0; i < dataList.size(); i++) {
- if(dataList.get(i).getPositon()>=0){
- ContactBean contactBean = new ContactBean();
- contactBean.setCheck(true);
- if(type==1){
- contactBean.setContactName(dataList.get(i).getContactName());
- List<String> strlist = new ArrayList<String>();
- strlist.add(dataList.get(i).getContactNum().get(dataList.get(i).getPositon()));
- contactBean.setContactNum(strlist);
- }else{
- contactBean.setContactName(dataList.get(i).getContactNum().get(dataList.get(i).getPositon()));
- List<String> strlist = new ArrayList<String>();
- strlist.add(dataList.get(i).getContactName());
- contactBean.setContactNum(strlist);
- }
- alist.add(contactBean);
- }
- }
- return alist;
- }
- @Override
- public void notifyDataSetChanged() {
- // TODO Auto-generated method stub
- super.notifyDataSetChanged();
- numTextView.setText("已选择" + getSelectItem().size() + "位联系人");
- }
- public void selectAll() {
- for (int i = 0; i < dataList.size(); i++) {
- dataList.get(i).setCheck(true);
- }
- notifyDataSetChanged();
- }
- public void cancelAll() {
- for (int i = 0; i < dataList.size(); i++) {
- dataList.get(i).setCheck(false);
- }
- notifyDataSetChanged();
- }
- public static class ViewHolder {
- private TextView nameTextView;
- private ListView listview;
- }
- public void setGridViewHeightBasedOnChildren(ListView listView) {
- // 获取GridView对应的Adapter
- if (listView == null) {
- return;
- }
- ListAdapter listAdapter = listView.getAdapter();
- if (listAdapter == null) {
- return;
- }
- int totalHeight = 0;
- for (int i = 0; i < listAdapter.getCount(); i++) {
- View listItem = listAdapter.getView(i, null, listView);
- listItem.measure(0, 0);
- totalHeight += listItem.getMeasuredHeight();
- }
- ViewGroup.LayoutParams params = listView.getLayoutParams();
- params.height = totalHeight
- + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
- listView.setLayoutParams(params);
- }
- }
- public class ContactsSameItemListAdapter extends BaseAdapter {
- private Context mContext;
- private ContactBean dataList;
- private HashSet<String> selectSect;
- private List<ContactBean> list;
- private int seposi;
- private TextView numTextView;
- public ContactsSameItemListAdapter(Context context, ContactBean list,
- List<ContactBean> mlist, int seposi, TextView textView,
- HashSet<String> selectSet) {
- this.mContext = context;
- this.dataList = list;
- this.list = mlist;
- this.seposi = seposi;
- this.numTextView = textView;
- this.selectSect = selectSet;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return dataList != null ? dataList.getContactNum().size() : 0;
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- final int pos = position;
- final ViewHolder viewHolder;
- if (convertView == null) {
- viewHolder = new ViewHolder();
- convertView = LayoutInflater.from(mContext).inflate(
- R.layout.item_contact_same_name_import_listview, null);
- viewHolder.nameTextView = (TextView) convertView
- .findViewById(R.id.contact_import_same_name_txt);
- viewHolder.checkBox = (CheckBox) convertView
- .findViewById(R.id.contact_import_same_name_checkbox);
- viewHolder.lineView = convertView.findViewById(R.id.view_line001);
- convertView.setTag(viewHolder);
- } else {
- viewHolder = (ViewHolder) convertView.getTag();
- }
- if (position == dataList.getContactNum().size() - 1) {
- viewHolder.lineView.setVisibility(View.GONE);
- } else {
- viewHolder.lineView.setVisibility(View.VISIBLE);
- }
- viewHolder.nameTextView.setText(dataList.getContactNum().get(position));
- viewHolder.checkBox.setOnCheckedChangeListener(null);
- if (selectSect.contains(dataList.getContactNum().get(position))) {
- viewHolder.checkBox.setChecked(true);
- viewHolder.checkBox.setButtonDrawable(R.drawable.xuanzhong);
- } else {
- viewHolder.checkBox.setChecked(false);
- viewHolder.checkBox.setButtonDrawable(R.drawable.xuankuang);
- }
- viewHolder.checkBox
- .setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- String regEx = "[^0-9]";
- Pattern p = Pattern.compile(regEx);
- Matcher m = p.matcher(numTextView.getText().toString());
- int num = Integer.parseInt(m.replaceAll("").trim());
- if (isChecked) {// 选中
- viewHolder.checkBox.setChecked(true);
- list.get(seposi).setPositon(pos);
- if (selectSect.size() == 0) {
- numTextView.setText("已选择" + (num + 1) + "位联系人");
- }
- selectSect.clear();
- selectSect.add(dataList.getContactNum().get(pos));
- notifyDataSetChanged();
- } else {
- list.get(seposi).setPositon(-1);
- selectSect
- .remove(dataList.getContactNum().get(pos));
- viewHolder.checkBox.setChecked(false);
- notifyDataSetChanged();
- numTextView.setText("已选择" + (num - 1) + "位联系人");
- }
- }
- });
- return convertView;
- }
- public static class ViewHolder {
- private TextView nameTextView;
- private CheckBox checkBox;
- private View lineView;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/bg_gray_login"
- android:orientation="vertical" >
- <View
- android:layout_width="fill_parent"
- android:layout_height="1dp"
- android:background="#A0A0A0"
- android:layout_marginTop="5dp"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/white"
- android:orientation="horizontal"
- android:weightSum="3" >
- <TextView
- android:id="@+id/contact_import_same_txt"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_marginLeft="15dp"
- android:layout_marginRight="15dp"
- android:layout_weight="1"
- android:gravity="center"
- android:singleLine="true"
- android:text="aa"
- android:textColor="@color/black" />
- <View
- android:layout_width="1dp"
- android:layout_height="match_parent"
- android:layout_marginTop="5dp"
- android:layout_marginBottom="5dp"
- android:background="#A0A0A0" />
- <ListView
- android:id="@+id/contact_import_same_listview"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="2"
- android:scrollbars="none" >
- </ListView>
- </LinearLayout>
- <View
- android:layout_width="fill_parent"
- android:layout_height="1dp"
- android:background="#A0A0A0"/>
- </LinearLayout>