ome*_*bal 0 checkbox android listview uncheck
我想在customlistview.my适配器中取消选中复选框工作正常,然后单击按钮
这是在我的按钮监听器中写的
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);
}
Run Code Online (Sandbox Code Playgroud)
在切换方法中
public void toggle(CheckBox v)
{
if (v.isChecked())
{
v.setChecked(false);
}
else
{
v.setChecked(true);
}
}
Run Code Online (Sandbox Code Playgroud)
适配器
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;
}
Run Code Online (Sandbox Code Playgroud)
这个代码在视图中的问题当我向下滚动时我得到6个孩子我再次得到6个孩子..当在视图中显示向上或向下滚动时,孩子是列表视图中的项目因此所示列表视图的项目是列表视图的子项. .so我希望所有的孩子都取消选中,但是这段代码没有用,请告诉我该怎么做?
inv*_*igo 11
这是取消选中ViewGroup的所有子CheckBox的简单方法(ListView扩展ViewGroup).您只想将ListView传递给此方法.
private void uncheckAllChildrenCascade(ViewGroup vg) {
for (int i = 0; i < vg.getChildCount(); i++) {
View v = vg.getChildAt(i);
if (v instanceof CheckBox) {
((CheckBox) v).setChecked(false);
} else if (v instanceof ViewGroup) {
uncheckAllChildrenCascade((ViewGroup) v);
}
}
}
Run Code Online (Sandbox Code Playgroud)