ListView with CheckBoxes and a click listener
<p>I would like to implement a click listener for the CheckBoxes of a ListView that uses a custom adapter. Like this:</p>

<p><img src="http://i.stack.imgur.com/wtur8.png" alt="enter image description here"></p>

<p>I know about the existence of the choiceMode, but I would like to implement myself.</p>

<p>I'm trying to apply good practices.
This is how my adapter looks like:</p>

<pre><code>public class TaskListAdapter extends ArrayAdapter&lt;ListTask&gt; {
    private ArrayList&lt;ListTask&gt; mItems;
    private ArrayList&lt;Boolean&gt; mChecked;
    private LayoutInflater mInflater;

    public TaskListAdapter(Context context, int textViewResourceId, ArrayList&lt;ListTask&gt; items) {
        super(context, textViewResourceId, items);
        this.mItems = items;

        // Cache the LayoutInflate to avoid asking for a new one each time
        mInflater = LayoutInflater.from(context);

    // Initialize all checkboxes with false value
        mChecked = new ArrayList&lt;Boolean&gt;();
        for (int i = 0; i &lt; this.getCount(); i++) {
        mChecked.add(i, false);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_row, null);

            holder = new ViewHolder();
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.mycheckbox);
            holder.text = (TextView) convertView.findViewById(R.id.mytext);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MyList mylist = mItems.get(position);

    holder.text.setText(mylist.getMyText());

        // Create a listener for the CheckBox
        holder.checkbox.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View v) {
            if (holder.checkbox.isChecked()) {
            mChecked.set(position, true);
            } else {
            mChecked.set(position, false);
            }
        }

        });
        holder.checkbox.setChecked(mChecked.get(position));

        return convertView;
    }

    static class ViewHolder {
        CheckBox checkbox;
        TextView text;
    }
}
</code></pre>

<p>When I run my app, I get this error:</p>

<blockquote>
  <p>java.lang.IndexOutOfBoundsException:
  Invalid location 0, size is 0</p>
</blockquote>

<p>That errors is thrown by the line</p>

<pre><code>holder.checkbox.setChecked(mChecked.get(position));
</code></pre>

<p>that is just before the return statement.</p>

<p>If I comment it and re-run the app, it shows the list, but when I click on a checkbox it throws also a IndexOutOfBoundsException for this line:</p>

<pre><code>mChecked.set(position, true);
</code></pre>

<p>that is inside the "if" statement that checks if the checkbox is checked.</p>

<p>What am I doing wrong?</p>
