Example usage for android.util LongSparseArray LongSparseArray

List of usage examples for android.util LongSparseArray LongSparseArray

Introduction

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

Prototype

public LongSparseArray() 

Source Link

Document

Creates a new LongSparseArray containing no mappings.

Usage

From source file:de.robbers.dashclock.stackextension.StackExtension.java

private void parseReputationResponse(String json) {
    if (json == null) {
        return;//from   w w  w  . ja va 2 s . co m
    }
    mExpandedBody = "";
    LongSparseArray reputationArray = new LongSparseArray();
    try {
        JSONArray items = new JSONObject(json).getJSONArray("items");
        Log.i(TAG, items.toString(2));

        for (int i = 0; i < items.length(); i++) {
            JSONObject reputation = items.getJSONObject(i);
            long postId = reputation.optLong("post_id");
            int reputationChange = reputation.optInt("reputation_change");
            int newValue = reputationChange;
            newValue += (Integer) reputationArray.get(postId, 0);
            reputationArray.put(postId, newValue);
        }

        List<Long> postIds = new ArrayList<Long>();
        for (int i = 0; i < items.length(); i++) {
            JSONObject reputation = items.getJSONObject(i);
            long postId = reputation.optLong("post_id");
            int reputationChange = reputation.optInt("reputation_change");
            if (postIds.contains(postId) || reputationChange == 0) {
                continue;
            }
            postIds.add(postId);
            int reputationValue = (Integer) reputationArray.get(postId);
            String title = String.valueOf(Html.fromHtml(reputation.optString("title")));
            mExpandedBody += buildExpandedBodyPost(reputationValue, title, postIds.size());
        }
    } catch (JSONException e) {
        Log.i(TAG, json);
        e.printStackTrace();
    }
    if (TextUtils.isEmpty(mExpandedBody)) {
        mExpandedBody = getString(R.string.no_recent_reputation_changes);
    }
}

From source file:com.example.twoway.TwoWayView.java

/**
 * Defines the choice behavior for the List. By default, Lists do not have any choice behavior
 * ({@link #CHOICE_MODE_NONE}). By setting the choiceMode to {@link #CHOICE_MODE_SINGLE}, the
 * List allows up to one item to  be in a chosen state. By setting the choiceMode to
 * {@link #CHOICE_MODE_MULTIPLE}, the list allows any number of items to be chosen.
 *
 * @param choiceMode One of {@link #CHOICE_MODE_NONE}, {@link #CHOICE_MODE_SINGLE}, or
 * {@link #CHOICE_MODE_MULTIPLE}/*from w w w . java2s  .c om*/
 */
public void setChoiceMode(ChoiceMode choiceMode) {
    mChoiceMode = choiceMode;

    if (mChoiceMode.compareTo(ChoiceMode.NONE) != 0) {
        if (mCheckStates == null) {
            mCheckStates = new SparseBooleanArray();
        }

        if (mCheckedIdStates == null && mAdapter != null && mAdapter.hasStableIds()) {
            mCheckedIdStates = new LongSparseArray<Integer>();
        }
    }
}

From source file:com.yktx.check.listview.TwoWayView.java

/**
 * Defines the choice behavior for the List. By default, Lists do not have any choice behavior
 * ({@link #CHOICE_MODE_NONE}). By setting the choiceMode to {@link #CHOICE_MODE_SINGLE}, the
 * List allows up to one item to  be in a chosen state. By setting the choiceMode to
 * {@link #CHOICE_MODE_MULTIPLE}, the list allows any number of items to be chosen.
 *
 * @param choiceMode One of {@link #CHOICE_MODE_NONE}, {@link #CHOICE_MODE_SINGLE}, or
 * {@link #CHOICE_MODE_MULTIPLE}/*from w w w .j  av a 2 s . c om*/
 */
@SuppressLint("NewApi")
public void setChoiceMode(ChoiceMode choiceMode) {
    mChoiceMode = choiceMode;

    if (mChoiceMode.compareTo(ChoiceMode.NONE) != 0) {
        if (mCheckStates == null) {
            mCheckStates = new SparseBooleanArray();
        }

        if (mCheckedIdStates == null && mAdapter != null && mAdapter.hasStableIds()) {
            mCheckedIdStates = new LongSparseArray<Integer>();
        }
    }
}

From source file:com.example.twoway.TwoWayView.java

@Override
public void setAdapter(ListAdapter adapter) {
    if (mAdapter != null && mDataSetObserver != null) {
        mAdapter.unregisterDataSetObserver(mDataSetObserver);
    }/*ww  w. j  a va 2s.  c o  m*/

    resetState();
    mRecycler.clear();

    mAdapter = adapter;
    mDataChanged = true;

    mOldSelectedPosition = INVALID_POSITION;
    mOldSelectedRowId = INVALID_ROW_ID;

    if (mCheckStates != null) {
        mCheckStates.clear();
    }

    if (mCheckedIdStates != null) {
        mCheckedIdStates.clear();
    }

    if (mAdapter != null) {
        mOldItemCount = mItemCount;
        mItemCount = adapter.getCount();

        mDataSetObserver = new AdapterDataSetObserver();
        mAdapter.registerDataSetObserver(mDataSetObserver);

        mRecycler.setViewTypeCount(adapter.getViewTypeCount());

        mHasStableIds = adapter.hasStableIds();
        mAreAllItemsSelectable = adapter.areAllItemsEnabled();

        if (mChoiceMode.compareTo(ChoiceMode.NONE) != 0 && mHasStableIds && mCheckedIdStates == null) {
            mCheckedIdStates = new LongSparseArray<Integer>();
        }

        final int position = lookForSelectablePosition(0);
        setSelectedPositionInt(position);
        setNextSelectedPositionInt(position);

        if (mItemCount == 0) {
            checkSelectionChanged();
        }
    } else {
        mItemCount = 0;
        mHasStableIds = false;
        mAreAllItemsSelectable = true;

        checkSelectionChanged();
    }

    checkFocus();
    requestLayout();
}

From source file:com.bmd.android.collection.LongSparseArrayTest.java

@Override
protected void setUp() throws Exception {

    super.setUp();

    if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN) {

        return;//from   w w  w. j a  va  2s .com
    }

    final LongSparseArray<String> array = new LongSparseArray<String>();

    for (int i = 0; i < 5; i++) {

        array.append(i, String.valueOf(i));
    }

    mArray = array;
}

From source file:com.example.twoway.TwoWayView.java

@Override
public Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();
    SavedState ss = new SavedState(superState);

    if (mPendingSync != null) {
        ss.selectedId = mPendingSync.selectedId;
        ss.firstId = mPendingSync.firstId;
        ss.viewStart = mPendingSync.viewStart;
        ss.position = mPendingSync.position;
        ss.height = mPendingSync.height;

        return ss;
    }/*from ww  w  . ja  v a2 s.c  o m*/

    boolean haveChildren = (getChildCount() > 0 && mItemCount > 0);
    long selectedId = getSelectedItemId();
    ss.selectedId = selectedId;
    ss.height = getHeight();

    if (selectedId >= 0) {
        ss.viewStart = mSelectedStart;
        ss.position = getSelectedItemPosition();
        ss.firstId = INVALID_POSITION;
    } else if (haveChildren && mFirstPosition > 0) {
        // Remember the position of the first child.
        // We only do this if we are not currently at the top of
        // the list, for two reasons:
        //
        // (1) The list may be in the process of becoming empty, in
        // which case mItemCount may not be 0, but if we try to
        // ask for any information about position 0 we will crash.
        //
        // (2) Being "at the top" seems like a special case, anyway,
        // and the user wouldn't expect to end up somewhere else when
        // they revisit the list even if its content has changed.

        View child = getChildAt(0);
        ss.viewStart = (mIsVertical ? child.getTop() : child.getLeft());

        int firstPos = mFirstPosition;
        if (firstPos >= mItemCount) {
            firstPos = mItemCount - 1;
        }

        ss.position = firstPos;
        ss.firstId = mAdapter.getItemId(firstPos);
    } else {
        ss.viewStart = 0;
        ss.firstId = INVALID_POSITION;
        ss.position = 0;
    }

    if (mCheckStates != null) {
        ss.checkState = cloneCheckStates();
    }

    if (mCheckedIdStates != null) {
        final LongSparseArray<Integer> idState = new LongSparseArray<Integer>();

        final int count = mCheckedIdStates.size();
        for (int i = 0; i < count; i++) {
            idState.put(mCheckedIdStates.keyAt(i), mCheckedIdStates.valueAt(i));
        }

        ss.checkIdState = idState;
    }

    ss.checkedItemCount = mCheckedItemCount;

    return ss;
}