Example usage for android.util LongSparseArray get

List of usage examples for android.util LongSparseArray get

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public E get(long key, E valueIfKeyNotFound) 

Source Link

Document

Gets the Object mapped from the specified key, or the specified Object if no such mapping has been made.

Usage

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

private void parseReputationResponse(String json) {
    if (json == null) {
        return;/*from  w  w  w  .  j a va 2s.c o 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);
    }
}