Android通讯录开发之实现全选、反选功能,主流app开发工具

// 用于保存勾选的联系人

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 ContextAndroid通讯录开发之实现全选、反选功能,主流app开发工具_第1张图片
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 int getCount() {

return contactList.size();

}

@Override

public Object getItem(int position) {

return contactList.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

你可能感兴趣的:(程序员,架构,移动开发,android)