Android在listview添加checkbox实现单选多选操作问题
android根据View的不同状态更换不同的背景
android 模仿朋友网推出的菜单效果【改进版】
让服务器iis支持.apk文件下载的设置方法
在 android 某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作。表面上看上去只是改变checkbox那么简单,然而实际开 发中,实现起来并不是那么得心应手。尤其当listview比较多(比如屏幕最多只能显示10个item,但总共有12个item,也就是说 listview的item数大于屏幕能够显示的item数)滑动屏幕的时候,由于适配器中getview()会重复使用被移除屏幕的item,所以会造 成checkbox选择状态不正常的现象。自己在开发中碰到这样的问题很是苦恼,查了下资料,发现网上很少没有针对这类批量操作并没有一个完整的例子。搜 了很多篇帖子才完美的实现这一常用的操作。所以在这里把这个Demo贴出来,供大家参考,希望能对大家有所帮助。
主界面的布局main.xml 这个就不多说什么ViewHolder类
package simtice.test.listview.viewholder; import android.widget.CheckBox; import android.widget.TextView; public class ViewHolder { public TextView tv = null; public CheckBox cb = null; }
为listview自定义适配器,该类为主Activity类MainActivity.java的内部类
public static class MyAdapter extends BaseAdapter { public static HashMapisSelected; private Context context = null; private LayoutInflater inflater = null; private List > list = null; private String keyString[] = null; private String itemString = null; // 记录每个item中textview的值 private int idValue[] = null;// id值 public MyAdapter(Context context, List > list, int resource, String[] from, int[] to) { this.context = context; this.list = list; keyString = new String[from.length]; idValue = new int[to.length]; System.arraycopy(from, 0, keyString, 0, from.length); System.arraycopy(to, 0, idValue, 0, to.length); inflater = LayoutInflater.from(context); init(); } // 初始化 设置所有checkbox都为未选择 public void init() { isSelected = new HashMap (); for (int i = 0; i < list.size(); i++) { isSelected.put(i, false); } } @Overridepublic int getCount() { return list.size(); } @Overridepublic Object getItem(int arg0) { return list.get(arg0); } @Overridepublic long getItemId(int arg0) { return 0; } @Overridepublic View getView(int position, View view, ViewGroup arg2) { ViewHolder holder = null; if (holder == null) { holder = new ViewHolder(); if (view == null) { view = inflater.inflate(R.layout.listviewitem, null); } holder.tv = (TextView) view.findViewById(R.id.item_tv); holder.cb = (CheckBox) view.findViewById(R.id.item_cb); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } HashMap map = list.get(position); if (map != null) { itemString = (String) map.get(keyString[0]); holder.tv.setText(itemString); } holder.cb.setChecked(isSelected.get(position)); return view; } }
最后,最重要的就是MainActivity.java中一些事件响应的处理
public class MainActivity extends Activity { TextView tv = null; ListView lv = null; Button btn_selectAll = null; Button btn_inverseSelect = null; Button btn_calcel = null; String name[] = { "G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9", "G10", "G11", "G12", "G13", "G14" }; ArrayListlistStr = null; private List > list = null; private MyAdapter adapter; @Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) this.findViewById(R.id.tv); lv = (ListView) this.findViewById(R.id.lv); btn_selectAll = (Button) this.findViewById(R.id.selectall); btn_inverseSelect = (Button) this.findViewById(R.id.inverseselect); btn_calcel = (Button) this.findViewById(R.id.cancel); showCheckBoxListView(); //全选 btn_selectAll.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { listStr = new ArrayList (); for(int i=0;i ); } adapter.notifyDataSetChanged();//注意这一句必须加上,否则checkbox无法正常更新状态 tv.setText("已选中"+listStr.size()+"项"); } }); //反选 btn_inverseSelect.setOnClickListener(new OnClickListener(){ @Overridepublic void onClick(View v) { for(int i=0;i ); } else{ MyAdapter.isSelected.put(i, false); listStr.remove(name ); } } adapter.notifyDataSetChanged(); tv.setText("已选中"+listStr.size()+"项"); } }); //取消已选 btn_calcel.setOnClickListener(new OnClickListener(){ @Overridepublic void onClick(View v) { for(int i=0;i ); } } adapter.notifyDataSetChanged(); tv.setText("已选中"+listStr.size()+"项"); } }); } // 显示带有checkbox的listview public void showCheckBoxListView() { list = new ArrayList >(); for (int i = 0; i < name.length; i++) { HashMap map = new HashMap (); map.put("item_tv", name ); map.put("item_cb", false); list.add(map); adapter = new MyAdapter(this, list, R.layout.listviewitem, new String[] { "item_tv", "item_cb" }, new int[] { R.id.item_tv, R.id.item_cb }); lv.setAdapter(adapter); listStr = new ArrayList (); lv.setOnItemClickListener(new OnItemClickListener() { @Overridepublic void onItemClick(AdapterView arg0, View view, int position, long arg3) { ViewHolder holder = (ViewHolder) view.getTag(); holder.cb.toggle();// 在每次获取点击的item时改变checkbox的状态 MyAdapter.isSelected.put(position, holder.cb.isChecked()); // 同时修改map的值保存状态 if (holder.cb.isChecked() == true) { listStr.add(name[position]); } else { listStr.remove(name[position]); } tv.setText("已选中"+listStr.size()+"项"); } }); } } //为listview自定义适配器内部类 public static class MyAdapter extends BaseAdapter { ... } }
效果图:
我选择了G2、G3、G11三项,现在屏幕滑动到底部,看以看到状态保存的很好,TextView显示已选中3项。全选、反选、取消全选功能正常,多选操作完美解决!
【转载】http://www.cnblogs.com/vus520/archive/2012/09/06/2673147.html