Search code examples
androidlistactivity

How to clear all checkboxes in ListActivity - or how to access each shown row?


I used ListActivity to create rows with checkboxes.

How can I clear or select all boxes at once AFTER the list has been created initially?

During creation in getView I just have the line:

holder.checkbox.setChecked(false);

But how can I access all the shown rows after they have been created outside getView?

This is how I create each row:

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.rowbuttonlayout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
        viewHolder.checkbox
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
                XXX element = (XXX) viewHolder.checkbox.getTag();
                element.selectedFlag = buttonView.isChecked() ? Constants.STATUS_SELECTED
                    : Constants.STATUS_UNSELECTED;
            }
            });
        view.setTag(viewHolder);
        viewHolder.checkbox.setTag(list.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.checkbox.setChecked(false);

    return view;
    }
}

Thanks!


Solution

  • Refer the following link :

    the perfect solution for your

    http://www.androiddom.com/2011/02/android-shopping-cart-tutorial.html

    See the code of ProductAdapter class

    private boolean mShowCheckbox;
    

    Later:

    if(!mShowCheckbox) {
       item.productCheckbox.setVisibility(View.GONE);
      } else {
       if(curProduct.selected == true)
        item.productCheckbox.setChecked(true);
       else
        item.productCheckbox.setChecked(false);
      }