问题
i want to uncheckall checkbox in customlistview.my adapter works fine so on click button
this is written in my button listener
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);
}
in toggle method
public void toggle(CheckBox v)
{
if (v.isChecked())
{
v.setChecked(false);
}
else
{
v.setChecked(true);
}
}
Adapter
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;
}
the problem with this code in view i get 6 child when i scroll down i get again 6 child ..the childs are the item in listview when scrolling up or down which is shown in view so the items of listview shown is child of listview..so i want all child to uncheck but with this code it is not working please tell me how can i do it?
回答1:
This is a simple way to uncheck all the children CheckBoxes of a ViewGroup (ListView extends ViewGroup). You'll just want to pass your ListView into this method.
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);
}
}
}
回答2:
One problem is where you do:
CheckBox cv =(CheckBox)v.findViewById(R.id.checktitle)
;
Your trying to get the checkbox associated with "v", but findViewById()
doesn't do that. Instead your just creating a NEW checkbox from your XML.
I think what you want to do is modify the underlying data "dcates" and then call notifyDataSetChanged()
on your adapter.
Something like:
for (CustomDishMenus menu : dcates)
menu.setSelected(false);
listview.getAdapter().notifyDataSetChanged();
回答3:
In your adapter (customAdapter) include a method call setAllSelected();
setAllSelected () {
for (int i = 0; i < this.getCount(); i++) {
AdapterItem info = this.getItem(i);
// info (add a boolean in you adapter item to store the state of checkbox i.e. isSelected and mark it to true. )
}
notifyDataSetChanged();
}
In your getView()
. according to state of variable(isSelected), select or deselect the checkbox.
Hope this helps.
来源:https://stackoverflow.com/questions/14509552/uncheck-all-checbox-in-listview-in-android