转自:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=152037
主界面的布局main.xml 这个就不多说什么
package simtice.test.listview.viewholder;
import android.widget.CheckBox;
import android.widget.TextView;
public class ViewHolder {
public TextView tv = null;
public CheckBox cb = null;
}
public static class MyAdapter extends BaseAdapter {
public static HashMap isSelected;
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) {
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
init();
}
// 初始化 设置所有checkbox都为未选择
public void init() {
isSelected = new HashMap();
for (int i = 0; i < list.size(); i++) {
isSelected.put(i, false);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int arg0) {
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public 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();
}
if (map != null) {
holder.tv.setText(list.get(position));
}
holder.cb.setChecked(isSelected.get(position));
return view;
}
}
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" };
ArrayList listStr = null;
private List> list = null;
private MyAdapter adapter;
@Override
public 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>();
for (int i = 0; i < name.length; i++) {
HashMap map = new HashMap();
map.put("item_tv", name[i]);
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() {
@Override
public void onItemClick(AdapterView> arg0, View view,
int position, long arg3) {
ViewHolder holder = (ViewHolder) view.getTag();
// 在每次获取点击的item时改变checkbox的状态
holder.cb.toggle();
// 同时修改map的值保存状态
MyAdapter.isSelected.put(position, holder.cb.isChecked());
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项。全选、反选、取消已选功能正常,多选操作完美解决!
ListViewForCheckbox.rar(65.9 KB, 下载次数: 246)
另:http://blog.sina.com.cn/s/blog_79014b2301017ejd.html