ListView的全选、全不选、反选、显示、删除
运行效果
与下图相似,新增删除按钮
自定义Bean类
/**
* 定义数据显示的Bean
*/
public class Bean {
public String id;
public String title;
public String desc;
public int m=1;
/**\
* isCheck 标记是否被选中
* isCheck属性,该属性主要标志CheckBox是否选中
*/
public boolean isCheck;
public Bean(String id, String title, String desc) {
this.id = id;
this.title = title;
this.desc = desc;
}
}
适配器
public class MyAdapter extends BaseAdapter {
private Context context;
private List<Bean> beanList;
private LayoutInflater inflater;
public boolean flag = false;
private Bean bean;
public MyAdapter(Context context, List<Bean> beanList) {
this.context = context;
this.beanList = beanList;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return beanList.size();
}
@Override
public Object getItem(int position) {
return beanList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView==null){
convertView = inflater.inflate(R.layout.list_item_date,null);
viewHolder = new ViewHolder();
viewHolder.checkBox = convertView.findViewById(R.id.checkbox_id);
viewHolder.title_text = convertView.findViewById(R.id.text_title);
viewHolder.desc_text = convertView.findViewById(R.id.text_desc);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
bean = beanList.get(position);
if (bean!=null){
viewHolder.title_text.setText(bean.title);
viewHolder.desc_text.setText(bean.desc);
// 根据isSelected来设置checkbox的显示状况
if (flag){
viewHolder.checkBox.setVisibility(View.VISIBLE);
}else {
viewHolder.checkBox.setVisibility(View.GONE);
}
viewHolder.checkBox.setChecked(bean.isCheck);
//注意这里设置的不是onCheckedChangListener,还是值得思考一下的