网上Google了一下,发现都是用自定义界面后,CheckBox.setChcked(true);这个方法只能选定显示条目的状态。再者当我们要GetAllChecked 条目时,根本不会记录那个已经选定(除了显示的),只能自己写了。这里主要用到了自定义一个CheckIDs 的ArrayList 来保存我确实已经选定的ID,注意只有ID才是唯一的(发现也就是data数据集的位置),没有细致考究,有空再考究。
package Main.JockeyCalendar.Control;import java.util.ArrayList; import java.util.HashMap;import java.util.List;import java.util.Map; import android.content.Context; import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.CheckBox; import android.widget.SimpleAdapter;public class CheckBoxAdapter extends SimpleAdapter {private ArrayList< View> ListItemControls;//ListView View 的保存(一般只有屏幕显示的数目,因为是android 机制为了省资源是重用回View的)private ArrayList<Long> CheckedIDs;//***********注意,主要作用。这儿作为ItemD的保存(虽然是由getItemId方法获得,但发现是数据的Pos位置)private int mResource;//这里构造时保存资源protected LayoutInflater mInflater;//加载的容器 public int CheckBoxID;//是筛选框的res id/** * 返回已经选定的条目数 * @return */public int GetCheckedCounts(){ return CheckedIDs.size();}/** * 设置列表中指定的控件为选定 * @param iStart 开始位置 * @param iEnd 结束位置 * @param IsChecked True是选定,False为不选定 */public void SetCheckeds(int iStart,int iEnd,Boolean IsChecked){ if (this.ListItemControls==null || this.ListItemControls.size()==0|| CheckBoxID<0) return; for (int i=0;i<this.ListItemControls.size();i++){ if (i>=iStart && i<=iEnd){ SetChecked(i,IsChecked); } } this.notifyDataSetChanged();//同步显示 }/** * 设置列表中指定的控件为选定 * @param pos 位置 * @param IsChecked True是选定,False为不选定 */public void SetChecked(int pos,Boolean IsChecked){ Long item=this.getItemId(pos); if (CheckedIDs.contains(item)){//如果存在 if (IsChecked==false){//如果不是选定则移除 CheckedIDs.remove(item); } }else{//如果不存在 if(IsChecked) //如果是选定则添加 CheckedIDs.add(item); } if (pos<this.ListItemControls.size()){ View v=this.ListItemControls.get(pos); CheckBox ck=(CheckBox)v.findViewById(this.CheckBoxID); ck.setChecked(IsChecked); ck.toggle(); } }/** * 设置列表中指定的控件为选定 * @param ItemID * @param IsChecked True是选定,False为不选定 */public void SetChecked(Long ItemID,Boolean IsChecked){ if (CheckedIDs.contains(ItemID)){//如果存在 if (IsChecked==false){//如果不是选定则移除 CheckedIDs.remove(ItemID); } }else{//如果不存在 if(IsChecked) //如果是选定则添加 CheckedIDs.add(ItemID); } }/** * 设置列表中指定的控件为选定 * @param al 要选定的列表数组 * @param IsChecked True是选定,False为不选定 */public void SetCheckeds(ArrayList<Integer> al,Boolean IsChecked){ if (this.ListItemControls==null || this.ListItemControls.size()==0|| CheckBoxID<0) return; for (int i=0;i<this.ListItemControls.size();i++){ if (al.contains(i)) SetChecked(i,IsChecked); } this.notifyDataSetChanged();//同步显示}/** * 获得被选定的条目数据 * @return */public List<? extends Map<String, ?>> GetCheckedItems(){ if (this.CheckedIDs==null||this.CheckedIDs.size()<=0)return null; ArrayList<HashMap<String, Object>> d=new ArrayList<HashMap<String, Object>>(); for (int i=0;i<CheckedIDs.size();i++){ Integer l=Integer.valueOf(String.valueOf(CheckedIDs.get(i)));//这里有一个不安全的地方,就是getItemID() 这个方法是返回Long 的,我强制转成了Integer ,有可能溢出,但Integer的条目在手机已经足够使用了。先不考虑。 d.add((HashMap<String, Object>) this.getItem(l)); } return d;}/** * 获得已选定的ID用“,”分隔 * @return */public String GetCheckedIDs(){ ArrayList<HashMap<String, String>> al=(ArrayList<HashMap<String, String>>) GetCheckedItems(); String sid=""; if (al==null || al.size()<=0)return null; HashMap<String, String> hm; for(int i=0;i<al.size();i++){ hm=al.get(i); sid+="," + hm.get("ID"); } if (sid.length()>0) sid=sid.substring(1); return sid;} /** * 构造函数 * @param context * @param data * @param resource * @param from * @param to */ public CheckBoxAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); mResource=resource; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); ListItemControls=new ArrayList<View>(); CheckedIDs=new ArrayList<Long>(); // TODO Auto-generated constructor stub } private OnClickListener CKOCL=new OnClickListener(){//CheckBox 的点击事件 public void onClick(View v) { // TODO Auto-generated method stub Long id=(Long)v.getTag(); CheckBox ck=(CheckBox)v; SetChecked (id,ck.isChecked()); } }; @Override public View getView(int pos, View cv, ViewGroup p) { if (cv == null) { cv = mInflater.inflate(mResource, p, false); if (ListItemControls.size()<=pos) ListItemControls.add(cv);//添加显示的View }else{ if (ListItemControls.size()>pos) ListItemControls.set(pos, cv);//把这个父路对象保存,调用某个ID时就用findViewbyID的方法 } Long ItemID=this.getItemId(pos);//获得ItemID CheckBox ck=(CheckBox)cv.findViewById(this.CheckBoxID); ck.setOnClickListener(CKOCL);//绑定 ck.setTag(ItemID);//利用Tag保存这个条目的ItemID ck.setChecked(CheckedIDs.contains(ItemID));//如果存在则为TRUE,不存在则是FALSE return super.getView(pos, cv, p); } }下面是绑定数据,代码。
//生成适配器的Item和动态数组对应的元素 CheckBoxAdapter sa = new CheckBoxAdapter(this,al,//数据源 R.layout.personsavelistitem,//ListItem的XML实现 //动态数组与ImageItem对应的子项 new String[] {"SkyLoc","Info"}, //ImageItem的XML文件里面的一个ImageView,两个TextView ID new int[] {R.id.txtTitle,R.id.txtInfo} ); sa.CheckBoxID=R.id.chkSelect; LV.setAdapter(sa); LV.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?> parent, View view, int position, long id) { CheckBox ck=(CheckBox)view.findViewById(R.id.chkSelect); ck.toggle(); } });