我想取消选中 customlistview 中的所有复选框。我的适配器工作正常,所以点击按钮
这是写在我的按钮监听器中的
for(int i = 0; i<listview.getChildCount();i++)
{
v = listview.getChildAt(i);
CheckBox cv =(CheckBox)v.findViewById(R.id.checktitle);
if(cv.isChecked())
{
// cv.setChecked(false);
//listview.setItemChecked(i, false);
toggle(cv);
}
在切换方法中
public void toggle(CheckBox v)
{
if (v.isChecked())
{
v.setChecked(false);
}
else
{
v.setChecked(true);
}
}
适配器
public class customAdapter extends ArrayAdapter {
View view=null;
Context context;
ViewHolder holder; boolean checkAll_flag = false;
boolean checkItem_flag = false;
List<CustomDishMenus> dcates=new ArrayList<CustomDishMenus>();
public customAdapter(Context context, int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context=context;
this.dcates=objects;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
holder = new ViewHolder();
final CustomDishMenus ords=dcates.get(position);
LayoutInflater layoutInflater=(LayoutInflater) getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
convertView=layoutInflater.inflate(R.layout.tablayout,parent, false);
if(convertView!=null){
holder.text = (TextView) convertView.findViewById(R.id.title);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.checktitle);
holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int getPosition = (Integer) buttonView.getTag();
dcates.get(getPosition).setSelected(buttonView.isChecked());
}
});
convertView.setTag(holder);
convertView.setTag(R.id.title, holder.text);
convertView.setTag(R.id.checktitle, holder.checkbox);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setTag(position); // This line is important.
holder.text.setText(dcates.get(position).getDishName());
holder.checkbox.setChecked(dcates.get(position).isSelected());
return convertView;
}
这段代码的问题是当我向下滚动时我得到6个孩子我再次得到6个孩子..当向上或向下滚动时,孩子是listview中的项目,显示在视图中,因此显示的listview项目是listview的孩子。 .所以我希望所有孩子都取消选中,但是使用此代码无法正常工作请告诉我该怎么做?