最近在实现一个checkbox动态加载到listview中的问题,网上看到一个功能差不多的demo,我添加了一个more的button,具体代码如下:
public class MainActivity extends ActionBarActivity {
private ListView lv;
private TestAdapter mAdapter;
private ArrayList<String> list;
private Button more;
private Button bt_selectall;
private Button bt_cancel;
private Button bt_deselectall;
private int checkNum; // 记录选中的条目数量 辅助功能
private TextView tv_show;// 用于显示选中的条目数量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listview);
more = (Button)findViewById(R.id.more);
bt_selectall = (Button) findViewById(R.id.bt_selectall);
bt_cancel = (Button) findViewById(R.id.bt_cancelselectall);
bt_deselectall = (Button) findViewById(R.id.bt_deselectall);
tv_show = (TextView) findViewById(R.id.tv);
list = new ArrayList<String>();
// 为Adapter准备数据
initDate();
mAdapter = new TestAdapter(list, this);
lv.setAdapter(mAdapter);
more.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
initDate();
for (int i = 0; i < list.size(); i++) {
TestAdapter.getIsSelected().put(i, false);
}
mAdapter.notifyDataSetChanged();
}
});
bt_selectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
TestAdapter.getIsSelected().put(i, true);
}
checkNum = list.size();
dataChanged();
}
});
bt_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 遍历list的长度,将已选的按钮设为未选
for (int i = 0; i < list.size(); i++) {
if (TestAdapter.getIsSelected().get(i)) {
TestAdapter.getIsSelected().put(i, false);
checkNum--;// 数量减1
}
}
dataChanged();
}
});
bt_deselectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 遍历list的长度,将已选的设为未选,未选的设为已选
for (int i = 0; i < list.size(); i++) {
if (TestAdapter.getIsSelected().get(i)) {
TestAdapter.getIsSelected().put(i, false);
checkNum--;
} else {
TestAdapter.getIsSelected().put(i, true);
checkNum++;
}
}
// 刷新listview和TextView的显示
dataChanged();
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
ViewHolder holder = (ViewHolder) view.getTag();
// 改变CheckBox的状态
holder.cb.toggle();
// 将CheckBox的选中状况记录下来
TestAdapter.getIsSelected()
.put(position, holder.cb.isChecked());
// 调整选定条目
if (holder.cb.isChecked() == true) {
checkNum++;
} else {
checkNum--;
}
// 用TextView显示
tv_show.setText("已选中" + checkNum + "项");
}
});
}
// 初始化数据
private void initDate() {
for (int i = 0; i < 1; i++) {
list.add("data" + " " + i);
}
}
// 刷新listview和TextView的显示
private void dataChanged() {
// 通知listView刷新
mAdapter.notifyDataSetChanged();
// TextView显示最新的选中数目
tv_show.setText("已选中" + checkNum + "项");
}
}
适配器如下:
public class TestAdapter extends BaseAdapter {
// 填充数据的list
private ArrayList<String> list;
// 用来控制CheckBox的选中状况
private static HashMap<Integer, Boolean> isSelected;
// 上下文
private Context context;
// 用来导入布局
private LayoutInflater inflater = null;
// 构造器
public TestAdapter(ArrayList<String> list, Context context) {
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
isSelected = new HashMap<Integer, Boolean>();
// 初始化数据
initDate();
}
// 初始化isSelected的数据
private void initDate() {
for (int i = 0; i < list.size(); i++) {
getIsSelected().put(i, false);
}
}
@Override
public int getCount() {
return list.size();
}