Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android通訊錄開發之實現全選、反選功能

Android通訊錄開發之實現全選、反選功能

編輯:關於Android編程

Android通訊錄開發之實現全選、反選功能

2014年1月15日
實現全選、反選不是什麼難的事情,就只是用另外一個數據結構來存儲被選中的狀態,通過刷新列表來更新列表的顯示狀態。下面是實現效果:
\
定義一個散列表來存儲選中狀態<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPgo8cHJlIGNsYXNzPQ=="brush:java;">// 用於保存勾選的聯系人 private HashMap map_NumberSelected = new HashMap();
定義標識表示是否已經全選
private boolean isAllChecked = false;

在觸發事件的位置根據標識來實現全選或反選
if (isAllChecked) {
				selectAllBtn.setBackgroundResource(R.drawable.select_all_not);
				invertSelection();
			} else {
				selectAllBtn.setBackgroundResource(R.drawable.select_all);
				selectAllContacts();
			}


全選方法
public void selectAllContacts() {
		for (int i = 0; i < contactList.size(); i++) {
			Contact contact = contactList.get(i);
			map_NumberSelected.put(contact, true);
		}
		isAllChecked = true;
		refreshList();
	}

反選方法
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,它跟map_NumberSelected都通過adapter裡面定義的set方法注入到adapter裡,所以選中狀態發生變化也相當於數據源發生變化,只要調用notifyDataSetChanged()方法就可以更新列表。

下面是自定義adapter的代碼,更新選中的狀態在getView方法進行
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 lmap = new HashMap();
	private ArrayList contactList;
	private Context context;
	private HashMap 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 contactList,
			String kind) {
		this.contactList = contactList;
		this.context = context;
		map_NumberSelected = new HashMap();
		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 contactList) {
		this.contactList = contactList;
	}

	public void setMap_NumberSelected(
			HashMap map_NumberSelected) {
		this.map_NumberSelected = map_NumberSelected;
	}

}







  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved