Code example for SparseBooleanArray

Methods: clear, get, put, size, valueAt

0
     * 
     * @see #setChoiceMode(int) 
     */ 
    public boolean isItemChecked(int position) {
        if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
            return mCheckStates.get(position);
        } 
 
        return false; 
    } 
 
    /** 
     * Returns the set of checked items in the list. The result is only valid if 
     * the choice mode has not been set to {@link #CHOICE_MODE_NONE}. 
     * 
     * @return  A SparseBooleanArray which will return true for each call to 
     *          get(int position) where position is a position in the list, 
     *          or <code>null</code> if the choice mode is set to 
     *          {@link #CHOICE_MODE_NONE}. 
     */ 
    public SparseBooleanArray getCheckedItemPositions() {
        if (mChoiceMode != CHOICE_MODE_NONE) {
            return mCheckStates;
        } 
        return null; 
    } 
 
    /** 
     * Returns the set of checked items ids. The result is only valid if the 
     * choice mode has not been set to {@link #CHOICE_MODE_NONE} and the adapter 
     * has stable IDs. ({@link ListAdapter#hasStableIds()} == {@code true}) 
     * 
     * @return A new array which contains the id of each checked item in the 
     *         list. 
     */ 
    public long[] getCheckedItemIds() { 
        if (mChoiceMode == CHOICE_MODE_NONE || mCheckedIdStates == null) {
            return new long[0]; 
        } 
 
        final LongSparseArray<Integer> idStates = mCheckedIdStates;
        final int count = idStates.size();
        final long[] ids = new long[count];
 
        for (int i = 0; i < count; i++) {
            ids[i] = idStates.keyAt(i);
        } 
 
        return ids;
    } 
 
    /** 
     * Clear any choices previously set 
     */ 
    public void clearChoices() { 
        if (mCheckStates != null) {
            mCheckStates.clear();
        } 
        if (mCheckedIdStates != null) {
            mCheckedIdStates.clear();
        } 
        mCheckedItemCount = 0;
    } 
 
    /** 
     * Sets the checked state of the specified position. The is only valid if 
     * the choice mode has been set to {@link #CHOICE_MODE_SINGLE} or 
     * {@link #CHOICE_MODE_MULTIPLE}. 
     * 
     * @param position The item whose checked state is to be checked 
     * @param value The new checked state for the item 
     */ 
    public void setItemChecked(int position, boolean value) {
        if (mChoiceMode == CHOICE_MODE_NONE) {
            return; 
        } 
 
        if (isItemCheckable(position) == false) {
        	return; 
        } 
         
        if (mChoiceMode == CHOICE_MODE_MULTIPLE) {
            boolean oldValue = mCheckStates.get(position);
            mCheckStates.put(position, value);
            if (mCheckedIdStates != null && hasStableIds()) {
                if (value) {
                    mCheckedIdStates.put(getItemId(position), position);
                } else { 
                    mCheckedIdStates.delete(getItemId(position));
                } 
            } 
            if (oldValue != value) {
                if (value) {
                    mCheckedItemCount++;
                } else { 
                    mCheckedItemCount--;
                } 
            } 
            if (mMultipleChecksListener != null) {
                final long id = getItemId(position);
                mMultipleChecksListener.onCheckedStateChanged(this,
                        position, id, value);
            } 
        } else { 
            boolean updateIds = mCheckedIdStates != null && hasStableIds();
            // Clear all values if we're checking something, or unchecking the currently 
            // selected item 
            if (value || isItemChecked(position)) {
                mCheckStates.clear();
                if (updateIds) {
                    mCheckedIdStates.clear();
                } 
            } 
            // this may end up selecting the value we just cleared but this way 
            // we ensure length of mCheckStates is 1, a fact getCheckedItemPosition relies on 
            if (value) {
                mCheckStates.put(position, true);
                if (updateIds) {
                    mCheckedIdStates.put(getItemId(position), position);
                } 
                mCheckedItemCount = 1;
            } else if (mCheckStates.size() == 0 || !mCheckStates.valueAt(0)) {
                mCheckedItemCount = 0;
            } 
        } 
 
        notifyDataSetChanged();