Example usage for android.util SparseBooleanArray keyAt

List of usage examples for android.util SparseBooleanArray keyAt

Introduction

In this page you can find the example usage for android.util SparseBooleanArray keyAt.

Prototype

public int keyAt(int index) 

Source Link

Document

Given an index in the range 0...size()-1, returns the key from the indexth key-value mapping that this SparseBooleanArray stores.

Usage

From source file:org.bangbang.support.v4.widget.HListView.java

/**
     * Returns the set of checked items ids. The result is only valid if
     * the choice mode has not been set to {@link #CHOICE_MODE_SINGLE}.
     *//from  www.  ja  va2  s .  c  o m
     * @return A new array which contains the id of each checked item in the list.
     */
    public long[] getCheckItemIds() {
        if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null) {
            final SparseBooleanArray states = mCheckStates;
            final int count = states.size();
            final long[] ids = new long[count];
            final ListAdapter adapter = mAdapter;

            for (int i = 0; i < count; i++) {
                ids[i] = adapter.getItemId(states.keyAt(i));
            }

            return ids;
        }

        return new long[0];
    }

From source file:com.appunite.list.HorizontalListView.java

/**
 * 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}.
 * /*from w  ww.  j  a va  2  s  .  c  o m*/
 * @return A new array which contains the id of each checked item in the
 *         list.
 *         
 * @deprecated Use {@link #getCheckedItemIds()} instead.
 */
@Deprecated
public long[] getCheckItemIds() {
    // Use new behavior that correctly handles stable ID mapping.
    if (mAdapter != null && mAdapter.hasStableIds()) {
        return getCheckedItemIds();
    }

    // Old behavior was buggy, but would sort of work for adapters without stable IDs.
    // Fall back to it to support legacy apps.
    if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null) {
        final SparseBooleanArray states = mCheckStates;
        final int count = states.size();
        final long[] ids = new long[count];
        final ListAdapter adapter = mAdapter;

        int checkedCount = 0;
        for (int i = 0; i < count; i++) {
            if (states.valueAt(i)) {
                ids[checkedCount++] = adapter.getItemId(states.keyAt(i));
            }
        }

        // Trim array if needed. mCheckStates may contain false values
        // resulting in checkedCount being smaller than count.
        if (checkedCount == count) {
            return ids;
        } else {
            final long[] result = new long[checkedCount];
            System.arraycopy(ids, 0, result, 0, checkedCount);

            return result;
        }
    }
    return new long[0];
}