android listview with actionmode
<p>In my android app, there is listview which gets populated from cursor(db). It has multiple choice enabled and actionbarsherlock's actionmode with actions on selected items. </p>

<p>I am stuck at this scenario,</p>

<p>I select two items from listview and action mode says two items selected. Now if I click back, then action mode finishes but checkeditem stays checked. How to uncheck all of them on back key keeping in mind that listview.getChildCount() will only return visible items?</p>

<pre><code>    private static SparseBooleanArray checkedItemsArray;

    @Override
public void onItemClick(AdapterView&lt;?&gt; arg0, View v, int position, long arg3)
{
        boolean isOneRowChecked = false;
    CheckableRelativeLayout r;
    checkedItemsArray = listView.getCheckedItemPositions();

    for (int i = 0; i &lt; listView.getChildCount(); i++)
    {
        r = (CheckableRelativeLayout) listView.getChildAt(i);

        if (r != null &amp;&amp; r.isChecked())
        {
            r.setBackgroundColor(Color.DKGRAY);
            isOneRowChecked = true;
        } else
        {
            r.setBackgroundColor(Color.BLACK);
        }
    }
    mActionMode = OtherActivity.this.startActionMode(mActionModeCallback);

    if (!isOneRowChecked &amp;&amp; mActionMode != null)
    {
        mActionMode.finish();
        checkedItemsArray = null;
    }

}
</code></pre>

<p>My activity is also implementing OnScrollListener, so</p>

<pre><code>    @Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
    int i = firstVisibleItem;
    if (checkedItemsArray != null)
    {
        for (int k = 0; k &lt; visibleItemCount; k++)
        {

            CheckableRelativeLayout r = (CheckableRelativeLayout) listView.getChildAt(k);
            if (checkedItemsArray.get(i))
            {
                r.setChecked(true);
                r.setBackgroundColor(Color.DKGRAY);
            } else
            {
                r.setChecked(false);
                r.setBackgroundColor(Color.BLACK);
            }
            i++;
        }
    }
    else
    {
        for (int k = 0; k &lt; visibleItemCount; k++)
        {
            listView.setItemChecked(k, false);
            CheckableRelativeLayout r = (CheckableRelativeLayout) listView.getChildAt(k);
            r.setChecked(false);
            r.setBackgroundColor(Color.BLACK);
        }
    }
}
</code></pre>
