android 功能模块之通讯模块

你猜 阅读:1284 2021-04-01 00:24:48 评论:0

Android通讯录开发之实现全选、反选功能


2014年1月15日 

实现全选、反选不是什么难的事情,就只是用另外一个数据结构来存储被选中的状态,通过刷新列表来更新列表的显示状态。下面是实现效果:



定义一个散列表来存储选中状态
  1. // 用于保存勾选的联系人  
  2.     private HashMap<Contact, Boolean> map_NumberSelected = new HashMap<Contact, Boolean>();  
// 用于保存勾选的联系人 
	private HashMap<Contact, Boolean> map_NumberSelected = new HashMap<Contact, Boolean>();

定义标识表示是否已经全选
  1. private boolean isAllChecked = false;  
private boolean isAllChecked = false;

在触发事件的位置根据标识来实现全选或反选
  1. if (isAllChecked) {  
  2.                 selectAllBtn.setBackgroundResource(R.drawable.select_all_not);  
  3.                 invertSelection();  
  4.             } else {  
  5.                 selectAllBtn.setBackgroundResource(R.drawable.select_all);  
  6.                 selectAllContacts();  
  7.             }  
if (isAllChecked) { 
				selectAllBtn.setBackgroundResource(R.drawable.select_all_not); 
				invertSelection(); 
			} else { 
				selectAllBtn.setBackgroundResource(R.drawable.select_all); 
				selectAllContacts(); 
			}


全选方法
  1. public void selectAllContacts() {  
  2.         for (int i = 0; i < contactList.size(); i++) {  
  3.             Contact contact = contactList.get(i);  
  4.             map_NumberSelected.put(contact, true);  
  5.         }  
  6.         isAllChecked = true;  
  7.         refreshList();  
  8.     }  
public void selectAllContacts() { 
		for (int i = 0; i < contactList.size(); i++) { 
			Contact contact = contactList.get(i); 
			map_NumberSelected.put(contact, true); 
		} 
		isAllChecked = true; 
		refreshList(); 
	}

反选方法
  1. public void invertSelection() {  
  2.         for (int i = 0; i < contactList.size(); i++) {  
  3.             Contact contact = contactList.get(i);  
  4.             map_NumberSelected.put(contact, false);  
  5.         }  
  6.         isAllChecked = false;  
  7.         refreshList();  
  8.     }  
public void invertSelection() { 
		for (int i = 0; i < contactList.size(); i++) { 
			Contact contact = contactList.get(i); 
			map_NumberSelected.put(contact, false); 
		} 
		isAllChecked = false; 
		refreshList(); 
	}


上面用到contactList是你得到的数据源,是一个List<Contact>,它跟map_NumberSelected都通过adapter里面定义的set方法注入到adapter里,所以选中状态发生变化也相当于数据源发生变化,只要调用notifyDataSetChanged()方法就可以更新列表。


下面是自定义adapter的代码,更新选中的状态在getView方法进行

  1. package com.suntek.mobilemeeting.adapter;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.content.Context;  
  7. import android.text.TextUtils;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11. import android.widget.BaseAdapter;  
  12. import android.widget.CheckBox;  
  13. import android.widget.TextView;  
  14.   
  15. import com.suntek.mobilemeeting.R;  
  16. import com.suntek.mobilemeeting.model.Contact;  
  17.   
  18. /** 
  19.  * 本地联系人适配器 
  20.  *  
  21.  * @author wwj 
  22.  *  
  23.  */  
  24. public class LocalContactAdapter extends BaseAdapter {  
  25.   
  26.     private HashMap<Integer, View> lmap = new HashMap<Integer, View>();  
  27.     private ArrayList<Contact> contactList;  
  28.     private Context context;  
  29.     private HashMap<Contact, Boolean> map_NumberSelected = null;  
  30.     private String kind;  
  31.   
  32.     public class ViewHolder {  
  33.         public TextView contactName;  
  34.         public TextView contactMobile;  
  35.         public TextView contactEmail;  
  36.         public CheckBox checkBox;  
  37.     }  
  38.   
  39.     public LocalContactAdapter(Context context, ArrayList<Contact> contactList,  
  40.             String kind) {  
  41.         this.contactList = contactList;  
  42.         this.context = context;  
  43.         map_NumberSelected = new HashMap<Contact, Boolean>();  
  44.         this.kind = kind;  
  45.     }  
  46.   
  47.     @Override  
  48.     public int getCount() {  
  49.         return contactList.size();  
  50.     }  
  51.   
  52.     @Override  
  53.     public Object getItem(int position) {  
  54.         return contactList.get(position);  
  55.     }  
  56.   
  57.     @Override  
  58.     public long getItemId(int position) {  
  59.         return position;  
  60.     }  
  61.   
  62.     @Override  
  63.     public View getView(int position, View convertView, ViewGroup parent) {  
  64.         ViewHolder holder;  
  65.         if (convertView == null) {  
  66.   
  67.             holder = new ViewHolder();  
  68.             LayoutInflater inflater = (LayoutInflater) context  
  69.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  70.             convertView = inflater.inflate(R.layout.local_contact_list_item,  
  71.                     null);  
  72.             holder.contactName = (TextView) convertView  
  73.                     .findViewById(R.id.name_tv);  
  74.             holder.contactMobile = (TextView) convertView  
  75.                     .findViewById(R.id.number_tv);  
  76.             holder.contactEmail = (TextView) convertView  
  77.                     .findViewById(R.id.email_tv);  
  78.             holder.checkBox = (CheckBox) convertView  
  79.                     .findViewById(R.id.local_contact_checkbox);  
  80.             // lmap.put(position, convertView);  
  81.             convertView.setTag(holder);  
  82.         } else {  
  83.             // convertView = lmap.get(position);  
  84.             holder = (ViewHolder) convertView.getTag();  
  85.         }  
  86.   
  87.         Contact contact = contactList.get(position);  
  88.         if (contact.getName() == "" || contact.getName() == null) {  
  89.             String name = "未知";  
  90.             holder.contactName.setText(name);  
  91.         } else {  
  92.             holder.contactName.setText(contact.getName());  
  93.         }  
  94.   
  95.         if (kind != null && kind.equals("email")) {  
  96.             if (!TextUtils.isEmpty(contact.getEmail())) {  
  97.                 holder.contactEmail.setText(contact.getEmail());  
  98.                 holder.contactMobile.setVisibility(View.GONE);  
  99.             }  
  100.         } else if (!TextUtils.isEmpty(contact.getNumber())) {  
  101.             holder.contactMobile.setText(contact.getNumber());  
  102.             holder.contactEmail.setVisibility(View.GONE);  
  103.         } else {  
  104.             holder.contactMobile.setVisibility(View.GONE);  
  105.             holder.contactEmail.setVisibility(View.GONE);  
  106.         }  
  107.         // 显示是否选中状态  
  108.         holder.checkBox.setChecked(map_NumberSelected.get(contact));  
  109.   
  110.         return convertView;  
  111.     }  
  112.   
  113.     public void setContactList(ArrayList<Contact> contactList) {  
  114.         this.contactList = contactList;  
  115.     }  
  116.   
  117.     public void setMap_NumberSelected(  
  118.             HashMap<Contact, Boolean> map_NumberSelected) {  
  119.         this.map_NumberSelected = map_NumberSelected;  
  120.     }  
  121.   
  122. }  
package com.suntek.mobilemeeting.adapter; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
 
import android.content.Context; 
import android.text.TextUtils; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.TextView; 
 
import com.suntek.mobilemeeting.R; 
import com.suntek.mobilemeeting.model.Contact; 
 
/** 
 * 本地联系人适配器 
 *  
 * @author wwj 
 *  
 */ 
public class LocalContactAdapter extends BaseAdapter { 
 
	private HashMap<Integer, View> lmap = new HashMap<Integer, View>(); 
	private ArrayList<Contact> contactList; 
	private Context context; 
	private HashMap<Contact, Boolean> map_NumberSelected = null; 
	private String kind; 
 
	public class ViewHolder { 
		public TextView contactName; 
		public TextView contactMobile; 
		public TextView contactEmail; 
		public CheckBox checkBox; 
	} 
 
	public LocalContactAdapter(Context context, ArrayList<Contact> contactList, 
			String kind) { 
		this.contactList = contactList; 
		this.context = context; 
		map_NumberSelected = new HashMap<Contact, Boolean>(); 
		this.kind = kind; 
	} 
 
	@Override 
	public int getCount() { 
		return contactList.size(); 
	} 
 
	@Override 
	public Object getItem(int position) { 
		return contactList.get(position); 
	} 
 
	@Override 
	public long getItemId(int position) { 
		return position; 
	} 
 
	@Override 
	public View getView(int position, View convertView, ViewGroup parent) { 
		ViewHolder holder; 
		if (convertView == null) { 
 
			holder = new ViewHolder(); 
			LayoutInflater inflater = (LayoutInflater) context 
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
			convertView = inflater.inflate(R.layout.local_contact_list_item, 
					null); 
			holder.contactName = (TextView) convertView 
					.findViewById(R.id.name_tv); 
			holder.contactMobile = (TextView) convertView 
					.findViewById(R.id.number_tv); 
			holder.contactEmail = (TextView) convertView 
					.findViewById(R.id.email_tv); 
			holder.checkBox = (CheckBox) convertView 
					.findViewById(R.id.local_contact_checkbox); 
			// lmap.put(position, convertView); 
			convertView.setTag(holder); 
		} else { 
			// convertView = lmap.get(position); 
			holder = (ViewHolder) convertView.getTag(); 
		} 
 
		Contact contact = contactList.get(position); 
		if (contact.getName() == "" || contact.getName() == null) { 
			String name = "未知"; 
			holder.contactName.setText(name); 
		} else { 
			holder.contactName.setText(contact.getName()); 
		} 
 
		if (kind != null && kind.equals("email")) { 
			if (!TextUtils.isEmpty(contact.getEmail())) { 
				holder.contactEmail.setText(contact.getEmail()); 
				holder.contactMobile.setVisibility(View.GONE); 
			} 
		} else if (!TextUtils.isEmpty(contact.getNumber())) { 
			holder.contactMobile.setText(contact.getNumber()); 
			holder.contactEmail.setVisibility(View.GONE); 
		} else { 
			holder.contactMobile.setVisibility(View.GONE); 
			holder.contactEmail.setVisibility(View.GONE); 
		} 
		// 显示是否选中状态 
		holder.checkBox.setChecked(map_NumberSelected.get(contact)); 
 
		return convertView; 
	} 
 
	public void setContactList(ArrayList<Contact> contactList) { 
		this.contactList = contactList; 
	} 
 
	public void setMap_NumberSelected( 
			HashMap<Contact, Boolean> map_NumberSelected) { 
		this.map_NumberSelected = map_NumberSelected; 
	} 
 
} 



标签:Android
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号